Search code examples
pythonregexpython-re

regex ":[^]" not working in python re module


I have a problem in regular expression using re module

pattern = ":[^]", string = ":r", and bool(re.findall(strting, pattern)) should return True However, it returns False like the pic1

I verified this using https://regexr.com/ and it shows like the pic2. So I believe the problem is on the re module

How can i show the same result of pic2 in python


Solution

  • That's the expected behavior cause re.findall(':r', ':[^]') means find the strings that match the pattern :r in the string :[^] i.e. the first argument is the pattern and second argument is the string/text where you need to find a match.

    And [^] in python regex means none of the characters that's inside the square brackets after caret ^ symbol should match.

    If you are looking to find the strings starting with : followed by any number of alphabets, following should work:

    >>> re.findall(':\w+',':r')
    [':r']
    ```