繁体   English   中英

我怎样才能得到这个正则表达式?

[英]How can I get this regex right?

我试图用@分割令牌,同时不允许特定字符。 我想出了这个

(@\w+[^ -+=(><)])

但它不能准确地工作! 假设我们的目标字符串是这样的:

 (() >= @a -5 and () <= @7 and ()= @127 and () = @1-7 and ()= @name_asd ()= @'hey this is a string') 

它没有得到单个字符的标记,而且它似乎接受了-作为一个有效的字符,而它不应该! 我还希望能够解析和匹配如下字符串:

@'something arbitrary with' 

也一样,但到目前为止无法弄清楚。

更新

我需要用@完全表示我想要的令牌

@a  For  () >= @a -5 
@7  For  () <= @7 
@127 For ()= @127
@1  For  () = @1-7  
@name_asd For  ()= @name_asd 
and 
@'hey this is a string' For ()= @'hey this is a string'

见演示:

@(?:\w+|'[^']*')

同时将您的-放在最后以避免形成不需要的范围。 见演示:

https://regex101.com/r/sJ9gM7/87

它没有获得single character tokens因为您在单个字符之后有一个空格,并且在\\w+之后不允许有空格。

来,试试这个:

(@[\w]+)

https://regex101.com/r/xH1fP3/1

它去掉了-但在字符串中留下了_

暂无
暂无

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

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