Skip to content

re.I does not work as expected #70542

Closed
Closed

Description

@MageshKumar
BPO 26354
Nosy @ezio-melotti
Superseder
  • bpo-11957: re.sub confusion between count and flags args
  • 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 2016-02-13.00:44:14.426>
    created_at = <Date 2016-02-13.00:19:24.626>
    labels = ['expert-regex', 'type-bug', 'invalid']
    title = 're.I does not work as expected'
    updated_at = <Date 2016-02-15.22:11:04.050>
    user = 'https://bugs.python.org/MageshKumar'

    bugs.python.org fields:

    activity = <Date 2016-02-15.22:11:04.050>
    actor = 'Magesh Kumar'
    assignee = 'none'
    closed = True
    closed_date = <Date 2016-02-13.00:44:14.426>
    closer = 'ezio.melotti'
    components = ['Regular Expressions']
    creation = <Date 2016-02-13.00:19:24.626>
    creator = 'Magesh Kumar'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 26354
    keywords = []
    message_count = 9.0
    messages = ['260216', '260219', '260220', '260328', '260329', '260330', '260331', '260334', '260335']
    nosy_count = 3.0
    nosy_names = ['ezio.melotti', 'mrabarnett', 'Magesh Kumar']
    pr_nums = []
    priority = 'normal'
    resolution = 'not a bug'
    stage = 'resolved'
    status = 'closed'
    superseder = '11957'
    type = 'behavior'
    url = 'https://bugs.python.org/issue26354'
    versions = ['Python 2.7']

    Activity

    1. MageshKumar commented on Feb 13, 2016

      MageshKumarmannequin
      MannequinAuthor

      I am in the process of re.sub the tag <item type="dict"> </item> with empty string from a xml output line.

      If "re.I" is used, I am not able to remove the complete tag.

      ========================================================================

      >>> a
      'ype="str">false</latency_statistics_enabled><name type="str">Default</name><aggregates type="list"><item type="dict"><aggregate type="list"><item type="dict"><type type="str">MulticastClient</item><is'
      
      >>> b = re.sub('\<\/?item(\s+type="dict")?\>', '', a, re.I)
      >>> b
      'ype="str">false</latency_statistics_enabled><name type="str">Default</name><aggregates type="list"><aggregate type="list"><type type="str">MulticastClient</item><is'
      >>> b = re.sub('\<\/?item(\s+type="dict")?\>', '', a)
      >>> b
      'ype="str">false</latency_statistics_enabled><name type="str">Default</name><aggregates type="list"><aggregate type="list"><type type="str">MulticastClient<is'

      ========================================================================

    2. mrabarnett commented on Feb 13, 2016

      mrabarnettmannequin
      Mannequin

      The 4th argument of re.sub is the count, not the flags.

      Not a bug.

    3. ezio-melotti commented on Feb 13, 2016

      @ezio-melotti
      Member
    4. MageshKumar commented on Feb 15, 2016

      MageshKumarmannequin
      MannequinAuthor

      Thanks for the inputs, It would be of great help, if someone could help me in explaining the below output :
      ========================================================================

      >>> a 
      'ype="str">false</latency_statistics_enabled><name type="str">Default</name><aggregates type="list"><item type="dict"><aggregate type="list"><root><type type="str">MulticastClient</root><is'
      >>> b = re.sub('\</?root\>', '', a, re.I)
      >>> b
      'ype="str">false</latency_statistics_enabled><name type="str">Default</name><aggregates type="list"><item type="dict"><aggregate type="list"><type type="str">MulticastClient<is'

      ========================================================================

    5. mrabarnett commented on Feb 15, 2016

      mrabarnettmannequin
      Mannequin

      The pattern '\</?root\>', which is the same as '</?root>', matches the string '<root>', and that is replaced with ''.

    6. MageshKumar commented on Feb 15, 2016

      MageshKumarmannequin
      MannequinAuthor

      :-)
      Thanks a lot Matthew for the inputs.

      If we compare the first example (<item>) and the <root> example, I am using re.I as the third element. But for the <root> example, still I am not to get the substitution happening correctly.

      Could you pls, let me know the reason of change in behaviour.

    7. MageshKumar commented on Feb 15, 2016

      MageshKumarmannequin
      MannequinAuthor

      Corrected Message :

      If we compare the first example (<item>) and the <root> example, I am using re.I as the third element. But for the <root> example, still I am able to get the substitution happening correctly.

      Could you pls, let me know the reason of change in behaviour.

    8. mrabarnett commented on Feb 15, 2016

      mrabarnettmannequin
      Mannequin

      The 3rd argument is the count (the maximum number of replacements, although 0 means no limit, not no replacements). You're passing in the flag re.I instead. re.I happens to have the numeric value 2, so you're telling it to do no more than 2 replacements.

    9. MageshKumar commented on Feb 15, 2016

      MageshKumarmannequin
      MannequinAuthor

      Thanks Matthew. :-)

    10. 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

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions