membersound
membersound

Reputation: 86627

How to match regex with end of line?

I'm trying to write a regex that does the following: - look for at least one =, and take these = up to either 1) end of line, or 2) a dot .

The regex:

[=]+?[=]+.*?[.$]+

Teststrings:

b == 123 //does not match, but which should as it is end of line!
b == 123. //does match "== 123.", which is OK
b == 123.abc //does match "== 123.", which is OK

What am I missing here with the endofline $ anchor?

Upvotes: 0

Views: 121

Answers (1)

kennytm
kennytm

Reputation: 523164

[.$] means a character class consisting of a dot or a dollar sign. If you want alternatives between regex elements you should use |, i.e. (\.|$).

Also, you could use negative character classes [^…] instead of lazy matching …*?:

([^=]+)=+([^.]+)(?:\.|$)

Upvotes: 2

Related Questions