Home > Back-end >  regex ":[^]" not working in python re module
regex ":[^]" not working in python re module

Time:09-12

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

CodePudding user response:

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']
```
  • Related