Need help with a Regular Expression, Pattern Matching in C#?
I need some help with a simple pattern matching and replacement exercise I am doing?
I need to match both of the following two strings in any string in a given context and it is expected that both patterns are to exist in a given supplied string.
1) "width=000" or "width=00" or "开发者_如何学编程width=0"
2) "drop=000" or "drop=00" or "drop=0"
The values can be any values between 0-9 for each case so '000' --> '999' could a valid test case in a supplied test.
string url = Regex.Replace(inputString, patternString, replacementValueString);
Thanks,
Have a look at this page to explain the individual elements: http://msdn.microsoft.com/en-us/library/az24scfc.aspx
A regex string like this should work great:
"\b(?:width|drop)\s*=\s*\d{1,3}\b"
To read the name and value in your code:
"\b(?<name>width|drop)\s*=\s*(?<value>\d{1,3})\b"
If you do not need to limit the numbers to only 3 digits, you could use the "\d+" instead of "\d{1,3}".
The "\b" at the beginning will make sure that you don't get a "width" or "drop" that is part of some larger word. The "\b" at the end will prevent you from matching numbers larger than 999.
The "\s*" on either side of the equals statement allow for "drop = 000" as well as "drop=000".
Something like this would work :
(?:width|drop)=\d{1,3}
精彩评论