gpt4 book ai didi

regex - 正则表达式 - 贪婪但在字符串匹配之前停止

转载 作者:行者123 更新时间:2023-12-02 06:39:31 25 4
gpt4 key购买 nike

我有一些数据,我想将其转换成表格格式。

这里是输入数据

1- This is the 1st line with a 
newline character
2- This is the 2nd line

每一行可能包含多个换行符。

输出

<td>1- This the 1st line with 
a new line character</td>
<td>2- This is the 2nd line</td>

我试过以下方法

^(\d{1,3}-)[^\d]*

但它似乎只匹配到 1 中的数字 1。

我希望在我的字符串中找到另一个\d{1,3}\- 后能够停止匹配。有什么建议吗?

编辑:我正在使用 EditPad Lite。

最佳答案

这是针对 vim 的,并使用 zerowidth positive-lookahead:

/^\d\{1,3\}-\_.*[\r\n]\(\d\{1,3\}-\)\@=

步骤:

/^\d\{1,3\}-              1 to 3 digits followed by -
\_.* any number of characters including newlines/linefeeds
[\r\n]\(\d\{1,3\}-\)\@= followed by a newline/linefeed ONLY if it is followed
by 1 to 3 digits followed by - (the first condition)

编辑:这就是它在 pcre/ruby 中的样子:

/(\d{1,3}-.*?[\r\n])(?=(?:\d{1,3}-)|\Z)/m

请注意,您需要一个以换行符结尾的字符串来匹配最后一个条目。

关于regex - 正则表达式 - 贪婪但在字符串匹配之前停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10774012/

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