Skip to content

It is undocumented that re.UNICODE and re.LOCALE affect re.IGNORECASE #69084

Closed

Description

@LeifArneStorset
BPO 24896
Nosy @jwilk, @ezio-melotti, @bitdancer, @serhiy-storchaka, @bricsuc
PRs
  • [2.7] bpo-24896: added re.UNICODE effects to insensitive flags. #1781
  • bpo-24896: Added effect of re.ASCII and reworded slightly #1782
  • bpo-24896 [3.6] Added effect of re.ASCII and reworded slightly (GH-1782) #3313
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2017-11-16.10:41:48.569>
    created_at = <Date 2015-08-19.12:38:11.284>
    labels = ['expert-regex', 'easy', 'type-feature', '3.7', 'docs']
    title = 'It is undocumented that re.UNICODE and re.LOCALE affect re.IGNORECASE'
    updated_at = <Date 2017-11-16.10:41:48.568>
    user = 'https://bugs.python.org/LeifArneStorset'

    bugs.python.org fields:

    activity = <Date 2017-11-16.10:41:48.568>
    actor = 'serhiy.storchaka'
    assignee = 'docs@python'
    closed = True
    closed_date = <Date 2017-11-16.10:41:48.569>
    closer = 'serhiy.storchaka'
    components = ['Documentation', 'Regular Expressions']
    creation = <Date 2015-08-19.12:38:11.284>
    creator = 'Leif Arne Storset'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 24896
    keywords = ['easy']
    message_count = 7.0
    messages = ['248829', '248837', '293730', '294324', '294410', '301305', '303823']
    nosy_count = 8.0
    nosy_names = ['jwilk', 'ezio.melotti', 'mrabarnett', 'r.david.murray', 'docs@python', 'serhiy.storchaka', 'Leif Arne Storset', 'Brian Ward']
    pr_nums = ['1781', '1782', '3313']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue24896'
    versions = ['Python 2.7', 'Python 3.6', 'Python 3.7']

    Activity

    1. LeifArneStorset commented on Aug 19, 2015

      LeifArneStorsetmannequin
      MannequinAuthor

      A non-ASCII string does not match a regular expression case-insensitively
      unless the UNICODE flag is set. This seems reasonable, but the documentation
      seems to imply that this is not the case.

      The example:

          import re
          # Does not match
          re.compile(u"неоднозначность", re.IGNORECASE) \
                  .findall(u"Неоднозначность") 
          # Matches
          re.compile(u"неоднозначность", re.IGNORECASE | re.UNICODE) \
                  .findall(u"Неоднозначность")

      (In Python 3, it does not match if re.ASCII is given.)

      The documentation (2.7) says:

      re.UNICODE
      
      Make \w, \W, \b, \B, \d, \D, \s and \S dependent on the Unicode character
      properties database.
      

      (https://docs.python.org/2/library/re.html#re.UNICODE)

      My regex does not use any of those escapes, yet the regex changes behavior with
      the UNICODE flag. This leads to confusion when the regex doesn't match. The documentation is very specific about the behavior that changes with the flag,
      implying that behavior not mentioned is unaffected.

      Of course, it's easy to guess the correct (hopefully) solution.

      Still, I suggest changing the documentation to mention that re.IGNORECASE is
      affected. Looking at the source code, there seems to be further consequences
      (it mentions "Unicode locale") which may also warrant a mention. If you do want
      to avoid specifics, however, even a hand-wavy reference to something like "match
      according to Unicode" would help, because it implies that not only the escapes
      change behavior.

      In Python 3, there is a counterpart to the 2.7 problem: re.ASCII makes our
      Cyrillic string not match. Again, this behavior makes intuitive sense, but the
      documentation seems to indicate something different:

      re.ASCII
      Make \w, \W, \b, \B, \d, \D, \s and \S perform ASCII-only matching instead
      of full Unicode matching. This is only meaningful for Unicode patterns, and
      is ignored for byte patterns.
      
      …
      
      re.IGNORECASE
      Perform case-insensitive matching; expressions like [A-Z] will match
      lowercase letters, too. This is not affected by the current locale and
      works for Unicode characters as expected.
      

      re.ASCII does appear to affect re.IGNORECASE. Since this is the non-default
      case, however, I'm not sure it's worth calling it out. I'd be happy even if
      only the 2.7 docs change.

    2. added
      docs
      on Aug 19, 2015
    3. bitdancer commented on Aug 19, 2015

      @bitdancer
      Member

      I think it would be reasonable to add re.IGNORECASE to the list of things affected, since it obviously does switch between using the unicode database and not doing so.

    4. added
      type-feature
      on Jan 4, 2016
    5. serhiy-storchaka commented on May 15, 2017

      @serhiy-storchaka
      Member

      As was reported in bpo-30373 there is the same issue with re.LOCALE.

      This documentation issue should be easy for everyone who is fluent in English and familiar with the re module.

    6. changed the title [-]It is undocumented that re.UNICODE affects re.IGNORECASE[/-] [+]It is undocumented that re.UNICODE and re.LOCALE affect re.IGNORECASE[/+] on May 15, 2017
    7. serhiy-storchaka commented on May 24, 2017

      @serhiy-storchaka
      Member

      Actually the locale affects case-insensitively matching if use the re.LOCAL flag. The set of characters matched by b'[A-Z]' is locale-depending. For example in Turkish locale it can include the letters 'İ' and 'ı'. Only 8-bit locales are supported, not UTF-8 locales.

      In Unicode case-insensitive mode the expression '[A-Z]' matches not only Latin uppercase and lowercacase letters A-Z and a-z, but also characters 'İ', 'ı', 'ſ', and 'K'.

    8. bricsuc commented on May 24, 2017

      bricsucmannequin
      Mannequin

      OK, I'll look at this soon and come up with the next iteration.

    9. serhiy-storchaka commented on Sep 5, 2017

      @serhiy-storchaka
      Member

      This wording is not correct as I noted in msg294324.

    10. serhiy-storchaka commented on Oct 6, 2017

      @serhiy-storchaka
      Member

      I tried to correct the documentation in bpo-31714.

    11. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      3.7 (EOL)docseasytopic-regextype-feature

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions