需要更多正则表达式调整建议

发布于 2024-12-07 01:43:52 字数 394 浏览 0 评论 0原文

感谢 SO 的帮助,我正在向大型参考文件添加分隔符。现在就快到了。我有一个从 SO 成员那里收到的正则表达式,它允许我添加“|”作者和年份之间。我知道需要添加“|”文章和出版物之间。我想出的规则是我想添加“|”在出版物名称中“:”之前的最后一个句点(点)之后。我尝试编辑我对此的积极展望,但似乎不起作用。我一如既往地感谢您对此的意见。提前致谢。

实际上我可能甚至不需要指定后面的 3 个“|”规则。主要的事情是匹配“:”之前的最后一个句点。那是正确的吗?即便如此,我的正则表达式似乎不起作用。

^((?:[^|]+\|){3}.*?\.)(?=\:)

122| Ryan, T.N. |2002. |Some article name here. Publication name 2: 101-105.

I'm getting through adding delimiters to a large references file thanks to help from SO. Almost there now. I have a regex which I received from an SO member which allowed me to add the "|" between the author and year. I know need to add "|" between the article and publication. The rule I cam up with is that I want to add the "|" after the last period(dot) that precedes the ":" in the publication name. I tried editing my positive look ahead for this but it does not seem to work. I would appreciate your input on this as always. Thanks in advance.

Actually I probably don't need even to specify the after 3 "|" rule. The main thing would be to match to the last period that precedes the ":". Would that be correct. Even so my regex does not seem to be working.

^((?:[^|]+\|){3}.*?\.)(?=\:)

122| Ryan, T.N. |2002. |Some article name here. Publication name 2: 101-105.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我不会写诗 2024-12-14 01:43:52

好吧,有两个问题:

  • 你忘记了在句号和冒号之间有一些东西用于前瞻! :)
  • 句点之前没有贪婪匹配,因此它匹配第一个句点而不是最后一个

尝试:

'^((?:[^|]+\|){3}.*\.)(?=[^:]*?\:)'

看到区别了吗? (删除了 ?,添加了 [^:]*

Okay 2 problems:

  • You forgot that there is stuff between the period and the colon for the lookahead! :)
  • You did not have a greedy match before the period, so it was matching the first period instead of the last

Try:

'^((?:[^|]+\|){3}.*\.)(?=[^:]*?\:)'

See the difference? (Removed a ?, added a [^:]*)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文