G J
G J

Reputation: 33

why isn't this being found with the specified regex in python?

I am using this regex in python:

=\s*[^(]([Tt]rue|[Ff]alse)

When I execute it, it is matching the items shown. However I also expect it to match the 7th item, because the beginning of the regex says an '=' followed by 0 or more white space characters (i.e spaces, tabs etc) and on line 7 there are 0 whites space characters. So why isn't it matching?

1 password = True (matched)
2 password = true (matched)
3 password = false (matched)
4 password = False (matched)
5 password "False"
6 password = 'True' (matched)
7 password =False (not matched but expected to be)
8 password =dict(required=False, default=None)

Upvotes: 0

Views: 68

Answers (3)

Barmar
Barmar

Reputation: 780871

[^(] means you require a character that isn't ( between the (possibly empty) sequence of spaces and the word True or False. There's no such character on that line.

You can make that character optional.

^[^(\n]*=\s*[^(]?([Tt]rue|[Ff]alse)

DEMO

Upvotes: 1

Bryan Oakley
Bryan Oakley

Reputation: 385910

The pattern is an equal sign =, followed by zero or more whitespace \s*, followed by any character other than '(' [^(], followed by the word true or false ([Tt]rue|[Ff]alse)

In item 7, the "any character other than (" pattern is matched by the letter F. After than is "alse" which isn't the word true and isn't the word false.

Upvotes: 2

user8426627
user8426627

Reputation: 943

[^(] 

ahh after edit its correct. that means one any not - [^'('] symbol after =\s* and THEN [Ff]alse, so it would batch only if some symbol is between = and [Ff]alse

Upvotes: 0

Related Questions