Reputation: 4113
I need to match any string that has certain characteristics, but I think enabling the /m
flag is breaking the functionality.
What I know:
My problem is, if I have the string twice in a single block of text, it returns once, matching everything between the first quote mark and last quote mark with the required words in-between.
Here is my regex:
/^"the[^@]*fox[^@]*lazy[^@]*"$/gim
Here is my understanding of the statement. Match where the string starts with "the
and there is the word fox
and lazy
(in that order) somewhere before the string ends with "
. Also ignore newlines and case-sensitivity.
The most common answer to limiting is (.*?)
But it doesn't work with new lines. And putting [^@?]*
doesn't work because it adds the ?
to the list of things to ignore.
So how can I keep the "match everything until ___" from skipping until the last instance while still being able to ignore newlines?
This is not a duplicate of anything else I can find because this deals with multi-line matching, and those don't.
Upvotes: 1
Views: 3481
Reputation: 2623
In your case, all your quantifiers need to be non-greedy so you can just use the flag ungreedy: U
.
/^"the[^@]*fox[^@]*lazy[^@]*"$/gimU
Upvotes: 3
Reputation: 4113
The answer, which was figured out while typing up this question, may seem ridiculously obvious.
Put the ?
after the *
, not inside the brackets. Parenthesis and Brackets are not analogous, and the ?
should be relative to the *
.
Corrected regex:
/^"the[^@]*?fox[^@]*?lazy[^@]*?"$/gim
The long and the short of this is:
[^@]*?
(substituting @
for something you don't want to match)
Upvotes: 1