RegEx for MyString然后是一个或多个CRLF,零个或多个行,然后是MyString2

时间:2017-09-13 12:38:05

标签: regex groovy

RegEx以确保配置文件中包含一些首选设置。 我的人类语言就是这样:

ntp一个{1个或更多个CRLF和任何字符} ntp两个| ntp两个{1个或更多个CRLF和任何字符} ntp一个

首先,我尝试使用RegEx101.com测试它,但最终的用法是groovy。

它必须符合以下所有这些可能性:

案例#1

ntp one
ntp two

案例2:

anything
ntp one
ntp two

案例3:

ntp one
1 or more lines of anything
ntp two

案例4:

ntp two
ntp one

案例5:

ntp two
1 or more lines of anything
ntp one

案例6:

ntp one
1 or more lines of anything
ntp two
1 or more lines with whatever

相反,它不应该与此相符:

ntp ten
ntp eleven

我的努力是因为我的努力......

ntp one[\s\S]ntp two|ntp two[\s\S]ntp one and ntp one.*ntp two|ntp two.*ntp one.  I don't know WHY this doesn't work - but I have very basic RegEx skills.

1 个答案:

答案 0 :(得分:0)

我会试一试:

(ntp one\n(.*?)ntp two)|(ntp two\n(.*?)ntp one)

它几乎都在说你写的东西。

  • 从ntp开始
  • 有一个新行
  • 一切都很好,一堆什么线(或没有)
  • 以ntp结尾

OR

  • 从ntp二开始
  • 有一个新行
  • 一切都很好,一堆线(或没有)
  • 以ntp结尾

这是Python中的一个工作示例:

import re

theregex = re.compile("(ntp one\n(.*?)ntp two)|(ntp two\n(.*?)ntp one)", re.DOTALL)
thestring = '''ntp one
1 or more lines of anything
ntp two'''

if re.search(theregex, thestring):
    print ("match")

根据实际实施情况,您需要使用您的语言使用DOTALL修饰符。这将使。 (DOT)匹配换行符。你在这里注意到我打开了:

https://regex101.com/r/MkfNPw/1

相关问题