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
2010-09-03 11:28:20
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
2010-09-03 11:29:57
@Younes = ^ char is stand for not
Pranay Rana
2010-09-03 11:35:02
^ char indicates line's beginning
iburlakov
2010-09-03 11:43:11
Thanks i didn't see that u had put a ^inside the bracket :)
Younes
2010-09-03 11:53:38
+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
2010-09-03 12:04:57
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
2010-09-03 12:52:14