正则表达式 匹配两个字符串之间的所有字符
例子: 这只是\一个简单的句子
。
我想匹配 This is
和 sentence
之间的每个字符。应忽略换行符。我无法弄清楚正确的语法。
Example:This is just\na 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(18)
例如
Regexr
我使用了lookbehind
(?<=)
并向前看(?=)
这样“This is”和“sentence”就不会包含在匹配中,但这取决于您的用例,您也可以简单地编写This is(.*)sentence< /代码>。
这里重要的是激活正则表达式引擎的“dotall”模式,以便
.
与换行符匹配。但如何执行此操作取决于您的正则表达式引擎。接下来的事情是您是否使用
.*
或.*?
。第一个是贪婪的,将匹配到字符串中的最后一个“句子”,第二个是惰性的,将匹配到字符串中的下一个“句子”。更新
Regexr
(?s) 打开 dotall 修饰符的位置,使得匹配换行符的
.
。更新 2:
与您的示例“这是(一个简单的)句子”匹配。请参阅此处 Regexr
For example
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 writeThis 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
Where the (?s) turns on the dotall modifier, making the
.
matching the newline characters.Update 2:
is matching your example "This is (a simple) sentence". See here on Regexr
需要惰性量词
复活这个问题,因为接受的答案中的正则表达式对我来说似乎不太正确。为什么?因为
将匹配我的第一句话。这是我的第二句话,这是我的第一句话。这是我的第二句话。
查看演示。
您需要在两个环视之间使用一个惰性量词。添加
?
会使星星变懒。这符合您的需求:
查看演示。我删除了不需要的捕获组。
跨换行符匹配的 DOTALL 模式
请注意,在演示中,设置了“点匹配换行符模式”(又名)dot-all(请参阅如何在各种语言中启用 DOTALL)。在许多正则表达式风格中,您可以使用在线修饰符
(?s)
进行设置,将表达式转换为:参考
Lazy Quantifier Needed
Resurrecting this question because the regex in the accepted answer doesn't seem quite correct to me. Why? Because
will match
my first sentence. This is my second
inThis 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:
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" (a.k.a.) 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:Reference
尝试
This is[\s\S]*?sentence
,适用于 javascriptTry
This is[\s\S]*?sentence
, works in javascript这:
在 JavaScript 中工作。
This:
works in javascript.
使用这个:
(?<=beginningstringname)(.*\n?)(?=endstringname)
use this:
(?<=beginningstringname)(.*\n?)(?=endstringname)
这对我有用(我正在使用 VS Code):
用于:
这只是\一个简单的句子
使用:
这个.+句子
This worked for me (I'm using VS Code):
for:
This is just\na simple sentence
Use:
This .+ sentence
您可以简单地使用:
\This is .*? \句子
You can simply use this:
\This is .*? \sentence
如果是 JavaScript,您可以使用
[^]
来 匹配包括换行符在内的任何字符。使用
/s
标志和点.
来匹配任何字符也可以,但应用于整个模式,并且 JavaScript 不支持 内联修饰符 打开/关闭该标志。要匹配尽可能少的字符,您可以通过附加问号来使量词非贪婪,并使用 捕获组以提取之间的部分。
请参阅 regex101 演示。
作为旁注,为了不匹配部分单词,您可以使用单词边界,例如
\bThis
和sentence\b
JavaScript 中的环视变体是
(?<=This is)[^]*?(?=sentence)
并且您可以检查 JS 正则表达式中的 Lookbehind 支持。另请参阅有关 Lookbehind 的重要说明。
In case of JavaScript you can use
[^]
to match any character including newlines.Using the
/s
flag with a dot.
to match any character also works, but is applied to the whole pattern and JavaScript does not support inline modifiers to turn on/off the flag.To match as least as possible characters, you can make the quantifier non greedy by appending a question mark, and use a capture group to extract the part in between.
See a regex101 demo.
As a side note, to not match partial words you can use word boundaries like
\bThis
andsentence\b
The lookaround variant in JavaScript is
(?<=This is)[^]*?(?=sentence)
and you could check Lookbehind in JS regular expressions for the support.Also see Important Notes About Lookbehind.
RegEx 使用 Java 方法匹配两个字符串之间的所有内容。
让我们使用 Pattern 和 Matcher 对象来使用 RegEx (.?)*。
由于 Matcher 可能包含多个匹配项,因此我们需要循环结果并存储它。
此示例将仅包含“将保存” 单词,但在较大的文本中,它可能会找到更多匹配项。
RegEx to match everything between two strings using the Java approach.
Let's use Pattern and Matcher objects to use RegEx (.?)*.
Since Matcher might contain more than one match, we need to loop over the results and store it.
This example will contain only "will save the" word, but in the bigger text it will probably find more matches.
如果有人正在 Jenkins 上下文中寻找这样的示例。它会解析 build.log,如果找到匹配项,则该匹配项会使构建失败。
In case anyone is looking for an example of this within a Jenkins context. It parses the build.log and if it finds a match it fails the build with the match.
有没有办法处理文本块中这种拆分的重复实例?例如:“这只是\一个简单的句子。这里有一些附加的东西。这只是\一个简单的句子。这里还有一些更多的东西。这只是\一个简单的句子。”。要匹配每个实例而不是整个字符串,请使用以下代码:
There is a way to deal with repeated instances of this split in a block of text? FOr instance: "This is just\na simple sentence. Here is some additional stuff. This is just\na simple sentence. And here is some more stuff. This is just\na simple sentence. ". to matches each instance instead of the entire string, use below code:
我是这样做的:
这对我来说比尝试找出所需的特定正则表达式更容易。
Here is how I did it:
This was easier for me than trying to figure out the specific regex necessary.
我在这里搜索正则表达式,以在旧脚本中的 Python2 中的 print“string”之间转换此打印语法:对于 Python3,使用 print(“string”)。效果很好,否则请使用 2to3.py 进行其他转换。这是我为其他人提供的解决方案:
在 Regexr.com 上尝试一下(由于某种原因在 NP++ 中不起作用):
对于变量:
对于标签和变量:
如何替换所有打印Python2 中的“string”与 Python3 中的 print(“string”) ?
I landed here on my search for regex to convert this print syntax between print "string", in Python2 in old scripts with: print("string"), for Python3. Works well, otherwise use 2to3.py for additional conversions. Here is my solution for others:
Try it out on Regexr.com (doesn't work in NP++ for some reason):
for variables:
for label and variable:
How to replace all print "string" in Python2 with print("string") for Python3?
对于 python
示例用法:
结果
[' is just\na simple ']
For python
Example usage:
Result
[' is just\na simple ']
您可以用空字符串替换不需要的位,而不是提取所需的位。
在红宝石中,
Rather than extract the bits you want you could replace the bits you don't want with empty strings.
In Ruby,
要在 VIM 中快速搜索,您可以使用
在 Vim 控制提示符处: /This is.*\_.*sentence
for a quick search in VIM, you could use
at Vim Control prompt: /This is.*\_.*sentence
我有这个字符串
,我想删除从
headers:
到content
的所有内容,所以我写了这个正则表达式(headers:)[^]*?(content )
并且它按预期工作,查找该表达式出现了多少次。
i had this string
and i wanted to remove everything from the words
headers:
tocontent
so I wrote this regex(headers:)[^]*?(content)
and it worked as expected finding how many times that expression has occurred.
Sublime Text 3x
在 Sublime Text 中,您只需写下您感兴趣的两个单词,例如在您的情况下,它是
“This is”和“sentence”
,然后在中间写 .*,即
This is .*entence< /code>
这应该对你有好处
Sublime Text 3x
In sublime text, you simply write the two word you are interested in keeping for example in your case it is
"This is" and "sentence"
and you write .* in between
i.e.
This is .* sentence
and this should do you well