Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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']
```
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading