Can someone help me write the regex for recognizing any hex value, I am struggling
Regex regexHexMatch = new Regex("\b0x[a-fA-F0-9]+\b");
if (regexHexMatch.IsMatch(data.ColorValue))
{
continue;
}
else
{
//Do something
}
Loading
Jignesh TrivediPosted Mar 26, 2012, 4:11 AM
ok,
Please try,
Regex r = new Regex(@"^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$");
var t = r.IsMatch("-4.70e+9");
hope this help.
David SmithPosted Mar 28, 2012, 8:13 AM
Inputs
var t = r.IsMatch("-1.70e+9"); This is dropping in the else statement as false
var t = r.IsMatch("3D"); This is dropping in the else statement as false
Expected Result: 1. This is what suppose to happen, the minute an inputvalue or regex matches an HEX value or not an number it needs drop in the true condition statement.
2. The minute the regex or inputvalues comes across an integer, float, or scientific notation ( Like "-1.70e+9") number, it should drop in the false statement.
Basically I want a regex that that matches any hex value or or anything thats not an number except for integer, float, or scientific notation ( Like "-1.70e+9") number. I am not for sure if this is much clearer or not.
var t = r.IsMatch("-1.70e+9"); This is dropping in the else statement as false
var t = r.IsMatch("3D"); This is dropping in the else statement as True
if (regexHexMatch.IsMatch(data.ColorValue))
{
continue;
}
else
{
//Do something
}
Jignesh TrivediPosted Mar 28, 2012, 12:35 AM
can please give more information.
actually i am confuse for your requirement.
David SmithPosted Mar 28, 2012, 12:04 AM
Regex r = new Regex(@"^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$");
Out put that i expect the regex to do. whereever you see false the the IsMatch function suppose to return true, whereever you see false the the IsMatch function suppose to return true,The regex is suppose to only return true if it comes across or the Value is an Hex value only or not an number, now if the value is scientific notation then it should return false, because for my case I want to process scientific notation values, at the moment the regex is returning false for both hex values and scientific notation values which is not the correct values. let me know if you understand
var t = r.IsMatch("-4.70e+9"); False
var t = r.IsMatch("+2.70e+9"); Falsevar t = r.IsMatch("-1.70e+9"); False
var t = r.IsMatch("3D"); True
var t = r.IsMatch("11FF"); True
if (regexHexMatch.IsMatch(data.Value))
{
//Hex values or not number only
;continue
}
else
{
//Scientific notation numbers only
false;
}
Jignesh TrivediPosted Mar 27, 2012, 1:58 AM
please refer
http://en.wikipedia.org/wiki/Regular_expression
hope you got your answer.
David SmithPosted Mar 26, 2012, 10:35 AM
also if you don't mind can break down the regex below step by step that you provided so I can understand.
Explain ^
Explain [-+]
Explain ?
Explain why the open close brackets
Explain $
Explain *\.
@"^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$"
David SmithPosted Mar 26, 2012, 3:52 AM
so really my regex needs to verity if the value is not a number, float or decimal and also if the value is a hex number, that way I can throw an error and report to my status log. keep in mind, the value can be ab scientific notation which is acceptable, scientific notation has an "E".
Can you help write the regex for this case above.
Jignesh TrivediPosted Mar 26, 2012, 2:10 AM
your color value may be #003344,#ff4455, ect
so please try,
Regex regexHexMatch = new Regex("^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$");
hope this help.