gpt4 book ai didi

java - 摩擦正则表达式 : matching a char except when after by another char

转载 作者:行者123 更新时间:2023-12-02 09:35:45 25 4
gpt4 key购买 nike

我需要找到一个正则表达式,该正则表达式可以按句点拆分字符串,该句点后面不紧接着右括号 (')'),并在拆分后保留分隔符 '.' .

例如,如果

str = "This is a great (feeling.) thing. It's very interesting."

正则表达式应该将其分成两个字符串:

["This is a great (feeling.) thing.", "It's very interesting."]

我尝试从 this并交换它但不成功。

感谢您的帮助。

最佳答案

所需的结果表明字符串将在句点后面的空格上分割(保留句点)。

str = "This is a great (feeling.) thing. It's very interesting."

str.split(/(?<=\.) +/)
# => ["This is a great (feeling.) thing.", "It's very interesting."]

(?<=\.)是一个积极的回顾".)"无关紧要。

为了回应下面OP的第二条评论,如果要在后面没有右括号的句点上分割字符串,则可以写:

str = "This is a great (feeling.) thing.It's very interesting."

str.split(/\.(?!\))/)
#=> ["This is a great (feeling.) thing", "It's very interesting"]

此正则表达式为“匹配后面不紧接右括号的句点”,(?!\) 消极前瞻

关于java - 摩擦正则表达式 : matching a char except when after by another char,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57532977/

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