Reputation:
I want to match a specific phrase finished or not with exclamation mark.
I made this regex that should match only Lorem Ipsum
and Lorem Ipsum!
:
/(?:Lorem Ipsum|Lorem Ipsum!)\b/
but it doesn't work with Lorem Ipsum!
(with exclamation mark).
EDIT: It should match only with Lorem Ipsum
and nothing else or Lorem Ipsum!
and nothing else, I mean it shouldn't match with Lorem Ipsum!eeeee
Upvotes: 0
Views: 1169
Reputation: 31712
Use this:
/Lorem Ipsum!?(?=\s|$)/g
it uses a lookahead to check if the character following the last character (!
or m
) is either a space \s
or the end of the line $
.
Note 1: Add the g
to match all occurences, and the i
modifier to ignore the case.
Upvotes: 1
Reputation: 189739
The reason /prefix|prefix with suffix/
doesn't work is that most regex engines prefer the leftmost match in an alternation. Simply reordering it should work.
However, as others have suggested, making just the exclamation mark optional is a hugely more readable solution.
/Lorem Ipsum!?$/
Since you apparently genuinely want to anchor to end of line, you should use $
at the end rather than \b
.
Upvotes: 0
Reputation: 11
Try with:
/lorem ipsum[!]?/
You can find a cheatsheet for regex and test them on: http://www.regexr.com
Upvotes: 1