Reputation: 294
My English skill is poor because i'm not a native English speaker. If rude expression exists in this article, please understand.
I am using regex library of .Net Core. I wanted to distinguish hexadecimal number and decimal number so I written a code to do this. But the code was not operated so I extract core logic that I want and created the below code on Test project newly.
string identPattern = "(?<rwC>[_a-zA-Z][_a-zA-Z0-9]*)";
string hexaPattern = "(?<iAZ>[0x][0-9]+)";
string decimalPattern = "(?<oKZ>[0-9]+)";
string pattern = string.Format("{0}|{1}|{2}", identPattern, hexaPattern, decimalPattern);
foreach (var data in Regex.Matches("0x10;", pattern, RegexOptions.Multiline | RegexOptions.ExplicitCapture))
{
var matchData = data as Match;
}
If it executes the above code "0" of "0x10" string is matched. This means "0x10" string is matched with decimalPattern ("(?[0-9]+)"). This is not result that I want. I want that "0x10" string is matched with hexaPattern.
Why don't matched with hexaPattern? and How to solve this problem?
My test code and execute result is as below.
Thanks for reading.
Upvotes: 0
Views: 182
Reputation: 2303
There is an error is in your hex pattern. you should not surround 0x
with []
as it means charset and will match only one character.
Try below for hex pattern
(?<iAZ>0x[0-9]+)
And if you don't want your decimal pattern to match your hex number, try adding ^
at the beginning of decimal pattern.
Upvotes: 2