Ruby 正则表达式匹配具有特殊条件的特定字符串

发布于 2024-08-27 09:13:35 字数 277 浏览 9 评论 0原文

我目前正在尝试借助正则表达式将文档解析为标记。

目前我正在尝试匹配文档中的关键字。例如我有以下文档:

Func test()
  Return blablaFuncblabla
EndFunc

需要匹配的关键字是Func、Return和EndFunc。

我想出了以下正则表达式: (\s|^)(Func)(\s|$) 来匹配 Func 关键字,但它并不完全按照我想要的方式工作,空格也匹配!

如何在不捕获空格的情况下匹配它?

I'm currently trying to parse a document into tokens with the help of regex.

Currently I'm trying to match the keywords in the document. For example I have the following document:

Func test()
  Return blablaFuncblabla
EndFunc

The keywords that needs to be matched is Func, Return and EndFunc.

I've comed up with the following regex: (\s|^)(Func)(\s|$) to match the Func keyword, but it doesn't work exactly like I want, the whitespaces are matched as well!

How can I match it without capturing the whitespaces?

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

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

发布评论

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

评论(1

陌伤ぢ 2024-09-03 09:13:35
(?:\s|^)(Func)(?:\s|$)

?: 使组不被捕获。

(?:\s|^)(Func)(?:\s|$)

?: makes a group non-capturing.

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