tags:

views:

32

answers:

2

I have a string which has to be matching @"^[\w*\$][\w\s-\$]*((\d{1,})){0,1}$". If it doesn't match this regex i want the characters that do not match to be deleted from the string. How can i set this up?

+3  A: 
s = Regex.Replace(s, @"^[^[\w*\$][\w\s-\$]*((\d{1,})){0,1}]$", "")
Pranay Rana
This will replace all characters from the regex above, i want the characters that are NOT in this regex to be replaced. This has to be reverted if possible.
Younes
@Younes = ^ char is stand for not
Pranay Rana
^ char indicates line's beginning
iburlakov
Thanks i didn't see that u had put a ^inside the bracket :)
Younes
+1  A: 

You probably want something like (but am not sure of the actual question). Maybe you want to remove the whole regex if it does not match, that's not what does the code below:

s = Regex.Replace(s, @"^[^\w*\$]([\w*\$])[^\w*\$\s-]*([\w\s-\$]*).*$", "$1$2")

The idea is to interleave each wanted character blocks with list of forbidden characters and keep only those that you want. The end of your regex was a bit strange, so I simplified it.

kriss
As you see I'm not the best with regular expressions. I am planning to go and dive into this topic soon. Thanks for your help.
Younes