Skip to content

re.I does not work as expected #70542

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
MageshKumar mannequin opened this issue Feb 13, 2016 · 9 comments
Closed

re.I does not work as expected #70542

MageshKumar mannequin opened this issue Feb 13, 2016 · 9 comments
Labels
topic-regex type-bug An unexpected behavior, bug, or error

Comments

@MageshKumar
Copy link
Mannequin

MageshKumar mannequin commented Feb 13, 2016

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']

    @MageshKumar
    Copy link
    Mannequin Author

    MageshKumar mannequin commented Feb 13, 2016

    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'

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

    @MageshKumar MageshKumar mannequin added topic-regex type-bug An unexpected behavior, bug, or error labels Feb 13, 2016
    @mrabarnett
    Copy link
    Mannequin

    mrabarnett mannequin commented Feb 13, 2016

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

    Not a bug.

    @ezio-melotti
    Copy link
    Member

    See bpo-11957

    @MageshKumar
    Copy link
    Mannequin Author

    MageshKumar mannequin commented Feb 15, 2016

    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'

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

    @mrabarnett
    Copy link
    Mannequin

    mrabarnett mannequin commented Feb 15, 2016

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

    @MageshKumar
    Copy link
    Mannequin Author

    MageshKumar mannequin commented Feb 15, 2016

    :-)
    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.

    @MageshKumar
    Copy link
    Mannequin Author

    MageshKumar mannequin commented Feb 15, 2016

    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.

    @mrabarnett
    Copy link
    Mannequin

    mrabarnett mannequin commented Feb 15, 2016

    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.

    @MageshKumar
    Copy link
    Mannequin Author

    MageshKumar mannequin commented Feb 15, 2016

    Thanks Matthew. :-)

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    topic-regex type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant