我正在使用Java并想构建两个适合两种不同情况的reg表达式:
1:
STARTText blah, blah
\ next line with more text, but the leading backslash
\ next line with more text, but the leading backslash
\ next line with more text, but the leading backslash
直到第一行不再以反斜杠开头。
2:
Now you will see the following links for the items:
1111 leading 4 digits and then some text
2565 leading 4 digits and then some text
8978 leading 4 digits and then some text
并且此块在例如8978。但是我还知道,以起始数字开头的块将重复10次,然后结束。
因此,以某种方式可以过滤单个行,但是如何在两个行之间有多个换行符呢?即使是第一个程序块,当我真的不知道何时/如何结束它时。还要搜索反斜杠。所以,我的方法是有一个封闭的表达式,只有一个-我也可以将其用于replaceAll()
最佳答案
正则表达式1:
/^STARTText.*?(\r?\n)(?:^\\.*?\1)+/m
现场演示:http://www.rubular.com/r/G35kIn3hQ4
正则表达式2:
/^.*?(\r?\n)(?:^\d{4}\s.*?\1)+/m
现场演示:http://www.rubular.com/r/TxFbBP1jLJ
编辑:
Java演示1:http://ideone.com/BPNrm6
Java中的正则表达式1:
(?m)^STARTText.*?(\\r?\\n)(?:^\\\\.*?\\1)+
Java演示2:http://ideone.com/TQB8Gs
Java中的Regex 2:
(?m)^.*?(\\r?\\n)(?:^\\d{4}\\s.*?\\1)+