Escaping problem using Regular Expression

H

Henry

I have this simple code,

string escaped = Regex.Escape( @"`~!@#$%^&*()_=+[]{}\|;:',<.>/?" + "\"" );
string input = @"a&+[b`${c}a'?sd:r]" + "\"" + @"@(-d)\e";
Regex re = new Regex( string.Format(@"([{0}]+)", escaped),
RegexOptions.CultureInvariant );
string s = re.Replace( input, "" );

It doesn't seem to work, regular expression return without filter out any
character
However if I remove or change the position of "]" to be the first character,
it works.

string escaped = Regex.Escape( @"]`~!@#$%^&*()_=+[{}\|;:',<.>/?" + "\"" );

I totally do not understand how this regular expression escaping works. What
am I doing wrong here?

Thanks
Henry
 
N

Niki Estner

Henry said:
I have this simple code,

string escaped = Regex.Escape( @"`~!@#$%^&*()_=+[]{}\|;:',<.>/?" + "\"" );
string input = @"a&+[b`${c}a'?sd:r]" + "\"" + @"@(-d)\e";
Regex re = new Regex( string.Format(@"([{0}]+)", escaped),
RegexOptions.CultureInvariant );
string s = re.Replace( input, "" );


Escaping rules within '[' ']''s are different: AFAIK only \], \- and control
characters (\r, \n...) have to be escaped in such a block.
It doesn't seem to work, regular expression return without filter out any
character


Try matching for new Regex(@"[`~!@#$%^&*()_=+[\]{}\|;:' said:
However if I remove or change the position of "]" to be the first character,
it works.

Yep, in "normal" regex context "]" is not a metacharacter, so you don't have
to escape it. That is, "Escape(...)" won't escape it either. That should
explain it.
I totally do not understand how this regular expression escaping works. What
am I doing wrong here?

Use "Escape(...)" if you match for an exact string. You can't put the
escaped sequence in a [..] block. That's it.

Niki
 
H

Henry

Thanks for answering.
Now I got it working. I modified my code as follows:

string excludeChars = @"`~!@#$%^&*()\-_=+[\]{}\\|;:',<.>/?\""";
string input = @"a&+[b`${c}a'?sd:r]" + "\"" + @"@(-d)\e";
Regex re = new Regex( string.Format(@"[{0}]+", excludeChars) );
string s = re.Replace( input, "" );


But I still don't really get what's the problem cause in my other code it
doesn't work as what my understanding.

string QuantifierRegex = @"(?=^[\w$#]{7,11}$)";
//string password = "`~!@#$%^&*";
string password = "abc4ddf";

Regex re1 = new Regex( QuantifierRegex );
bool isMatch1 = re1.IsMatch( password );

Regex re2 = new Regex( Regex.Escape(QuantifierRegex) );
bool isMatch2 = re2.IsMatch( password );

bool isMatch3 = Regex.IsMatch( password, QuantifierRegex,
RegexOptions.CultureInvariant );
bool isMatch4 = Regex.IsMatch( password, Regex.Escape(QuantifierRegex),
RegexOptions.CultureInvariant );

From the sample above,
isMatch1 returns true
isMatch2 returns false
isMatch3 returns true
isMatch4 returns false

If I change the password variable to "`~!@#$%^&*"
isMatch1 returns false
isMatch2 returns false
isMatch3 returns false
isMatch4 returns false

Any idea?
Thanks
Henry

Niki Estner said:
Henry said:
I have this simple code,

string escaped = Regex.Escape( @"`~!@#$%^&*()_=+[]{}\|;:',<.>/?" + "\"" );
string input = @"a&+[b`${c}a'?sd:r]" + "\"" + @"@(-d)\e";
Regex re = new Regex( string.Format(@"([{0}]+)", escaped),
RegexOptions.CultureInvariant );
string s = re.Replace( input, "" );


Escaping rules within '[' ']''s are different: AFAIK only \], \- and control
characters (\r, \n...) have to be escaped in such a block.
It doesn't seem to work, regular expression return without filter out any
character


Try matching for new Regex(@"[`~!@#$%^&*()_=+[\]{}\|;:' said:
However if I remove or change the position of "]" to be the first character,
it works.

Yep, in "normal" regex context "]" is not a metacharacter, so you don't have
to escape it. That is, "Escape(...)" won't escape it either. That should
explain it.
I totally do not understand how this regular expression escaping works. What
am I doing wrong here?

Use "Escape(...)" if you match for an exact string. You can't put the
escaped sequence in a [..] block. That's it.

Niki
 
N

Niki Estner

Henry said:
...
string QuantifierRegex = @"(?=^[\w$#]{7,11}$)";
//string password = "`~!@#$%^&*";
string password = "abc4ddf";

Regex re1 = new Regex( QuantifierRegex );
bool isMatch1 = re1.IsMatch( password );

re1 matches a string that consist of 7-11 characters, which may be
alphanumeric (\w), $ or #.
Don't know why you need the paranthesis.
Regex re2 = new Regex( Regex.Escape(QuantifierRegex) );

re2 matches for literally "(?=^[\w$#]{7,11}$)"
bool isMatch2 = re2.IsMatch( password );

bool isMatch3 = Regex.IsMatch( password, QuantifierRegex,
RegexOptions.CultureInvariant );
bool isMatch4 = Regex.IsMatch( password, Regex.Escape(QuantifierRegex),
RegexOptions.CultureInvariant );

From the sample above,
isMatch1 returns true
isMatch2 returns false
isMatch3 returns true
isMatch4 returns false

If I change the password variable to "`~!@#$%^&*"
isMatch1 returns false
isMatch2 returns false
isMatch3 returns false
isMatch4 returns false

Any idea?

Yes.
Looks correct.
What did you think it should do?

Niki
 
H

Henry H

Thanks for answering.
Now I got it working. I modified my code as follows:

string excludeChars = @"`~!@#$%^&*()\-_=+[\]{}\\|;:',<.>/?\""";
string input = @"a&+[b`${c}a'?sd:r]" + "\"" + @"@(-d)\e";
Regex re = new Regex( string.Format(@"[{0}]+", excludeChars) );
string s = re.Replace( input, "" );


But I still don't really get what's the problem cause in my other code
it doesn't work as what my understanding.

string QuantifierRegex = @"(?=^[\w$#]{7,11}$)";
//string password = "`~!@#$%^&*";
string password = "abc4ddf";

Regex re1 = new Regex( QuantifierRegex );
bool isMatch1 = re1.IsMatch( password );

Regex re2 = new Regex( Regex.Escape(QuantifierRegex) );
bool isMatch2 = re2.IsMatch( password );

bool isMatch3 = Regex.IsMatch( password, QuantifierRegex,
RegexOptions.CultureInvariant );
bool isMatch4 = Regex.IsMatch( password, Regex.Escape(QuantifierRegex),
RegexOptions.CultureInvariant );

From the sample above,
isMatch1 returns true
isMatch2 returns false
isMatch3 returns true
isMatch4 returns false

If I change the password variable to "`~!@#$%^&*"
isMatch1 returns false
isMatch2 returns false
isMatch3 returns false
isMatch4 returns false

Any idea?
Thanks
Henry

*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
 
H

Henry

Well, I totally forgot the (\w), $ or #. expression due to the 20 hours I'm
working on the project in a row.

Thanks so much

Niki Estner said:
Henry said:
...
string QuantifierRegex = @"(?=^[\w$#]{7,11}$)";
//string password = "`~!@#$%^&*";
string password = "abc4ddf";

Regex re1 = new Regex( QuantifierRegex );
bool isMatch1 = re1.IsMatch( password );

re1 matches a string that consist of 7-11 characters, which may be
alphanumeric (\w), $ or #.
Don't know why you need the paranthesis.
Regex re2 = new Regex( Regex.Escape(QuantifierRegex) );

re2 matches for literally "(?=^[\w$#]{7,11}$)"
bool isMatch2 = re2.IsMatch( password );

bool isMatch3 = Regex.IsMatch( password, QuantifierRegex,
RegexOptions.CultureInvariant );
bool isMatch4 = Regex.IsMatch( password, Regex.Escape(QuantifierRegex),
RegexOptions.CultureInvariant );

From the sample above,
isMatch1 returns true
isMatch2 returns false
isMatch3 returns true
isMatch4 returns false

If I change the password variable to "`~!@#$%^&*"
isMatch1 returns false
isMatch2 returns false
isMatch3 returns false
isMatch4 returns false

Any idea?

Yes.
Looks correct.
What did you think it should do?

Niki
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top