简体   繁体   English

Ruby regex将具有特殊条件的特定字符串匹配

[英]Ruby regex match specific string with special conditions

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. 需要匹配的关键字是Func,Return和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! 我想出了以下正则表达式:(\\ s | ^)(Func)(\\ s | $)匹配Func关键字,但是它不能完全按照我的要求工作,空格也被匹配!

How can I match it without capturing the whitespaces? 如何在不捕获空格的情况下进行匹配?

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

?:使群组不被捕获。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM