简体   繁体   English

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

[英]How can I get this regex right?

I am trying to split tokens with @ , and well meanwhile not allowing specific characters.我试图用@分割令牌,同时不允许特定字符。 I came up with this我想出了这个

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

But it doesn't work precisely!但它不能准确地工作! Suppose our target string is this :假设我们的目标字符串是这样的:

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

it doesn't get single character tokens, and also it seems to accept - as a valid character while it shouldn't!它没有得到单个字符的标记,而且它似乎接受了-作为一个有效的字符,而它不应该! I also wanted to be able to parse and match strings like:我还希望能够解析和匹配如下字符串:

@'something arbitrary with' 

as well, but couldn't figure it out so far.也一样,但到目前为止无法弄清楚。

Update更新

I need to get the token with @ altogether meaning I want我需要用@完全表示我想要的令牌

@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'

See demo:见演示:

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

Also place your - at the end to avoid forming an unwanted range.同时将您的-放在最后以避免形成不需要的范围。 See demo:见演示:

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

It wasn't getting single character tokens because you have a space after single character and you have not allowed a space after \\w+ .它没有获得single character tokens因为您在单个字符之后有一个空格,并且在\\w+之后不允许有空格。

Here, try this:来,试试这个:

(@[\w]+)

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

It cuts out the - but leaves the _ when in a string.它去掉了-但在字符串中留下了_

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

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