正则表达式捕获2个常量

时间:2018-01-25 18:36:43

标签: regex

我一直在寻找能够捕获2个常量之间的任何文本的正则表达式,例如:

//==Constant1==
-Any
-Other
-Text
//==Constant2==

我需要捕获整个块或至少Constant1和Constant2中的文本,我可以有换行符,数字,任何其他符号(反斜杠,parhentesis等)。我尝试过许多正则表达式:

\/\/==start==(.*)\/\/==end==/s

但那不起作用,有什么建议吗?

2 个答案:

答案 0 :(得分:3)

这适用于大多数口味

^//==Constant1==[\n\r]
([\s\S]+?)
^//==Constant2==

请参阅a demo on regex101.com(并注意multilineverbose修饰符!)。

<小时/> 分解,它说:

^//==Constant1==[\n\r]  # match //==Constant1== in one line
([\s\S]+?)              # anything else lazily in between
^//==Constant2==        # up until //==Constant2== in another line

根据您的口味,您可能需要将正斜杠转义为

^\/\/==Constant1==[\n\r]

答案 1 :(得分:2)

您需要在正则表达式中包含换行符。试试这个:\/\/==Constant1==\n(.+\n)*\/\/==Constant2==

提示:您可以随时在https://regex101.com/尝试使用正则表达式。

相关问题