简体   繁体   中英

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

It wasn't getting single character tokens because you have a space after single character and you have not allowed a space after \\w+ .

Here, try this:

(@[\w]+)

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

It cuts out the - but leaves the _ when in a string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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