问题
I'm trying to create a regex that will select an entire line where it contains a matching string.
I can't seem to get it to work.
Any help is greatly appreciated. You can see the test case and what I've tried here:
https://www.regex101.com/r/mT5rZ3/1
Many thanks in advance! :-)
回答1:
This answer solves the question with 463 steps instead of 952 steps. Just ensure a new line at the end of the file.
.*Eventname 2.*\n
https://www.regex101.com/r/mT5rZ3/5
回答2:
If you are using the PHP regex . don't match newlines. So
.*(\bEventname 2\b).*
would be enough. If . matches newline you would need *? to make the dots non-greedy (so it just matches one line, instead of everything). You also need to be in multi-line mode to use ^ and $, but that shouldn't be necessary (since you only want to match one line anyway).
回答3:
You are using a string containing several lines. By default, the ^ and $ operators will match the beginning and end of the whole string. The m modifier will cause them to match the beginning and end of a line.
回答4:
Try this:
(.*(?:Eventname 2).*)
explaination:
( ... ) : groups and captures the line
(?:...) : groups without capturing the string that the line needs to contain
.* : any characters
来源:https://stackoverflow.com/questions/31745545/regex-match-line-containing-string