Regex Match all characters between two strings
Example: "This is justna simple sentence".
I want to match every character between "This is" and "sentence". Line breaks should be ignored. I can't figure out the correct syntax.
For example
(?<=This is)(.*)(?=sentence)
Regexr
I used lookbehind (?<=)
and look ahead (?=)
so that "This is" and "sentence" is not included in the match, but this is up to your use case, you can also simply write This is(.*)sentence
.
The important thing here is that you activate the "dotall" mode of your regex engine, so that the .
is matching the newline. But how you do this depends on your regex engine.
The next thing is if you use .*
or .*?
. The first one is greedy and will match till the last "sentence" in your string, the second one is lazy and will match till the next "sentence" in your string.
Update
Regexr
This is(?s)(.*)sentence
Where the (?s) turns on the dotall modifier, making the .
matching the newline characters.
Update 2:
(?<=is ()(.*?)(?=s*))
is matching your example "This is (a simple) sentence". See here on Regexr
Lazy Quantifier Needed
Resurrecting this question because the regex in the accepted answer doesn't seem quite correct to me. Why? Because
(?<=This is)(.*)(?=sentence)
will match my first sentence. This is my second
my first sentence. This is my second
in This is my first sentence. This is my second sentence.
This is my first sentence. This is my second sentence.
See demo .
You need a lazy quantifier between the two lookarounds. Adding a ?
makes the star lazy.
This matches what you want:
(?<=This is).*?(?=sentence)
See demo. I removed the capture group, which was not needed.
DOTALL Mode to Match Across Line Breaks
Note that in the demo the "dot matches line breaks mode" (aka) dot-all is set (see how to turn on DOTALL in various languages). In many regex flavors, you can set it with the online modifier (?s)
, turning the expression into:
(?s)(?<=This is).*?(?=sentence)
Reference
试试This is[sS]*sentence
,用javascript工作
下一篇: 正则表达式匹配两个字符串之间的所有字符