我怎样才能正确使用这个正则表达式?(How can i get this regex right?)

编程入门 行业动态 更新时间:2024-10-10 05:25:20
我怎样才能正确使用这个正则表达式?(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')

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

@'sth arbitrary with'

同样,但到目前为止无法弄明白! 我很高兴知道如何解决这个问题。

更新 : 我需要使用@ altogether获取令牌,这意味着我想要的

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

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 :

@'sth arbitrary with'

as well, but couldn't figure it out so far! I would be grateful to know how to fix this.

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'

最满意答案

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

请参阅demo.Also place ur -最后避免形成不需要的范围。参见演示。

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

它没有获得single character tokens因为你在单个字符之后有一个空格而你在\w+之后没有允许空格。

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

See demo.Also place ur - at the end to avoid forming an unwanted range.See demo.

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

It wasnt getting single character tokens coz you have a space after single character and you have not allowed a space after \w+.

更多推荐

本文发布于:2023-08-03 19:32:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1398107.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:使用这个   正确   正则表达式   regex

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!