469,623 Members | 1,800 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,623 developers. It's quick & easy.

Escaping problem using Regular Expression

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
Jul 21 '05 #1
5 2040
"Henry" <ig******@hotmail.com> wrote in
news:en**************@TK2MSFTNGP11.phx.gbl...
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(@"[`~!@#$%^&*()_=+[\]{}\|;:',<.>/?" ]+")
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
Jul 21 '05 #2
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" <ni*********@cube.net> wrote in message
news:uA*************@tk2msftngp13.phx.gbl...
"Henry" <ig******@hotmail.com> wrote in
news:en**************@TK2MSFTNGP11.phx.gbl...
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(@"[`~!@#$%^&*()_=+[\]{}\|;:',<.>/?" ]+")
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

Jul 21 '05 #3
"Henry" <ig******@hotmail.com> wrote in
news:ec**************@TK2MSFTNGP12.phx.gbl...
...
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
Jul 21 '05 #4
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!
Jul 21 '05 #5
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" <ni*********@cube.net> wrote in message
news:eJ**************@TK2MSFTNGP12.phx.gbl...
"Henry" <ig******@hotmail.com> wrote in
news:ec**************@TK2MSFTNGP12.phx.gbl...
...
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

Jul 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Buddy | last post: by
3 posts views Thread by James D. Marshall | last post: by
11 posts views Thread by gisleyt | last post: by
6 posts views Thread by Lubomir | last post: by
7 posts views Thread by Matthew Warren | last post: by
reply views Thread by devrayhaan | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.