gpt4 book ai didi

java - 具有多行和特殊结构的字符串的正则表达式

转载 作者:行者123 更新时间:2023-11-29 05:42:58 25 4
gpt4 key购买 nike

我正在使用 Java 并想构建两个适合两种不同场景的正则表达式:

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

这个 block 以一个额外的空行结束,例如8978. 但另外我知道,起始数字的 block 将重复 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 中的正则表达式 2:

(?m)^.*?(\\r?\\n)(?:^\\d{4}\\s.*?\\1)+

关于java - 具有多行和特殊结构的字符串的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16857466/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com