Issue with regular expression
-
Hello, I have an issue in regular expression matching...
string x = "user = 'sa' password='eX65dere' server = 'localhost'";
Regex rx = new Regex("password=\'([a-zA-Z0-9]\\_+)\'");
var r = rx.Match(x);
if (r.Groups.Count > 0)
{
var g1 = r.Groups[0];
MessageBox.Show(g1.ToString());
}while executing this, I have got the following error message. parsing "password='([a-zA-Z0-9]\_+)'" - Unrecognized escape sequence \_. the following will work fine, when i try to add _ to the expression the exception occurs.
Regex rx = new Regex("password=\'([a-zA-Z0-9]+)\'");
Can anyone help me on this?
-
Hello, I have an issue in regular expression matching...
string x = "user = 'sa' password='eX65dere' server = 'localhost'";
Regex rx = new Regex("password=\'([a-zA-Z0-9]\\_+)\'");
var r = rx.Match(x);
if (r.Groups.Count > 0)
{
var g1 = r.Groups[0];
MessageBox.Show(g1.ToString());
}while executing this, I have got the following error message. parsing "password='([a-zA-Z0-9]\_+)'" - Unrecognized escape sequence \_. the following will work fine, when i try to add _ to the expression the exception occurs.
Regex rx = new Regex("password=\'([a-zA-Z0-9]+)\'");
Can anyone help me on this?
I have solved this by changing the expression as follows...
Regex rx = new Regex(@"password='([a-zA-Z0-9\\%^&_*]+)\'");
Regards Sebastian
-
Hello, I have an issue in regular expression matching...
string x = "user = 'sa' password='eX65dere' server = 'localhost'";
Regex rx = new Regex("password=\'([a-zA-Z0-9]\\_+)\'");
var r = rx.Match(x);
if (r.Groups.Count > 0)
{
var g1 = r.Groups[0];
MessageBox.Show(g1.ToString());
}while executing this, I have got the following error message. parsing "password='([a-zA-Z0-9]\_+)'" - Unrecognized escape sequence \_. the following will work fine, when i try to add _ to the expression the exception occurs.
Regex rx = new Regex("password=\'([a-zA-Z0-9]+)\'");
Can anyone help me on this?
Why are you escaping the underscore character? it is a normal character as far as Regex is concerned, just like + and - :)
Luc Pattyn [My Articles] Nil Volentibus Arduum
-
Hello, I have an issue in regular expression matching...
string x = "user = 'sa' password='eX65dere' server = 'localhost'";
Regex rx = new Regex("password=\'([a-zA-Z0-9]\\_+)\'");
var r = rx.Match(x);
if (r.Groups.Count > 0)
{
var g1 = r.Groups[0];
MessageBox.Show(g1.ToString());
}while executing this, I have got the following error message. parsing "password='([a-zA-Z0-9]\_+)'" - Unrecognized escape sequence \_. the following will work fine, when i try to add _ to the expression the exception occurs.
Regex rx = new Regex("password=\'([a-zA-Z0-9]+)\'");
Can anyone help me on this?
Simple. Underscore is not a metacharacter, so it doesn't need escaping (preceding with backslash). Cheers, Peter
Software rusts. Simon Stephenson, ca 1994.
-
Why are you escaping the underscore character? it is a normal character as far as Regex is concerned, just like + and - :)
Luc Pattyn [My Articles] Nil Volentibus Arduum
Snap!
Software rusts. Simon Stephenson, ca 1994.
-
Simple. Underscore is not a metacharacter, so it doesn't need escaping (preceding with backslash). Cheers, Peter
Software rusts. Simon Stephenson, ca 1994.
Got the idea, Thanks... again i have another question... Here is my updated expression...
string x = "user = 'sa' password='e X65dere!@#$%^&*()' server = 'localhost'";
Regex rx = new Regex(@"password=\'([a-zA-Z0-9\\!@#$%^&*|() _'""*-+{}<>,.;/?:~`\[\]\\\\]+)\'");
var r = rx.Match(x);
if (r.Groups.Count > 0)
{
var g1 = r.Groups[0];
MessageBox.Show(g1.ToString());
}In this case it will match for every small & large cap letters, numbers and special characters except = . If the password contains = the expression fails... can you please help?
-
Got the idea, Thanks... again i have another question... Here is my updated expression...
string x = "user = 'sa' password='e X65dere!@#$%^&*()' server = 'localhost'";
Regex rx = new Regex(@"password=\'([a-zA-Z0-9\\!@#$%^&*|() _'""*-+{}<>,.;/?:~`\[\]\\\\]+)\'");
var r = rx.Match(x);
if (r.Groups.Count > 0)
{
var g1 = r.Groups[0];
MessageBox.Show(g1.ToString());
}In this case it will match for every small & large cap letters, numbers and special characters except = . If the password contains = the expression fails... can you please help?
The reason is the quantifier + is greedy quantifier, that is it matches everything that matches the pattern up to the end of the input string. In your case it matches password='e X65dere!@#$%^&*()' server = 'localhost' when = is put in the pattern as every thing in between the first ' and last ' matches according to the pattern given when = included. To change this behaviour use the lazy quantifier that is +? instead of + alone.
-
The reason is the quantifier + is greedy quantifier, that is it matches everything that matches the pattern up to the end of the input string. In your case it matches password='e X65dere!@#$%^&*()' server = 'localhost' when = is put in the pattern as every thing in between the first ' and last ' matches according to the pattern given when = included. To change this behaviour use the lazy quantifier that is +? instead of + alone.
great help....worked...thanks
-
great help....worked...thanks
Thank you