Skip to main content
 首页 » 编程设计

c#之我怎样才能让这个正则表达式正确

2024年02月06日29zdz8207

我正在尝试使用 @ 拆分标记,同时不允许使用特定字符。我想出了这个

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

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

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

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

@'something arbitrary with'  

也是,但到目前为止还想不通。

更新

我需要用 @ 来获取 token ,这意味着我想要

@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

它没有得到单字符标记,因为单字符后有一个空格,而\w+ 后不允许有空格。