I have a string "this [is] not req!red"
i am trying to create a regex which will pick "characters" not specified in the list
[-a-zA-Z.,-()=\s\+:?!*\;><0-9]
any suggestions how to achieve this?
Language is C#
I have a string "this [is] not req!red"
i am trying to create a regex which will pick "characters" not specified in the list
[-a-zA-Z.,-()=\s\+:?!*\;><0-9]
any suggestions how to achieve this?
Language is C#
Your regex is a positive character class that matches each legal character specified in it:
[-a-zA-Z.,\-()=\s+:?!*\;><0-9]
The characters you don't want aren't matched. To switch this around, to match only the characters you want, make it a negative character class, by adding the ^
as its first character:
[^-a-zA-Z.,\-()=\s+:?!*\;><0-9]
To capture the matches, put the entire regex into a capturing group:
([-a-zA-Z.,\-()=\s+:?!*\;><0-9])
or
([^-a-zA-Z.,\-()=\s+:?!*\;><0-9])
Then iterate through the matches in your language of choice. Each character is in capture group one.
I'm uncertain of your goal. More information would help us give you better answers.
Please consider bookmarking the Stack Overflow Regular Expressions FAQ for future reference.
I think you need this :
[^-a-zA-Z.,\-()=\s\+:?!*\;><0-9]
the ^
will negate every character present in the list .
demo here : http://regex101.com/r/vX0oQ7
so basically in your example, it will not select anything apart from the square brackets .
this [is] not req!red
| |