472,967 Members | 1,914 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,967 software developers and data experts.

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 2241
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jane Doe | last post by:
Hello, I need to browse a list of hyperlinks, each followed by an author, and remove the links only for certain authors. 1. I searched the archives on Google, but didn't find how to tell the...
4
by: Buddy | last post by:
Can someone please show me how to create a regular expression to do the following My text is set to MyColumn{1, 100} Test I want a regular expression that sets the text to the following...
5
by: Ryan | last post by:
HELLO I am using the following MICROSOFT SUGGESTED (somewhere on msdn) regular expression to validate email addresses however I understand that the RFP allows for "+" symbols in the email address...
3
by: James D. Marshall | last post by:
The issue at hand, I believe is my comprehension of using regular expression, specially to assist in replacing the expression with other text. using regular expression (\s*) my understanding is...
5
by: Henry | last post by:
I have this simple code, string escaped = Regex.Escape( @"`~!@#$%^&*()_=+{}\|;:',<.>/?" + "\"" ); string input = @"a&+" + "\"" + @"@(-d)\e"; Regex re = new Regex( string.Format(@"(+)", escaped),...
11
by: gisleyt | last post by:
I need to escape e.g. & within an attribute, as in: <foo bar="+$"/> Since this is a regular expression and will not be validated as a normal xml file, i cannot use &amp; for &. CDATA does not seem...
6
by: Lubomir | last post by:
Hi, I am using the following pattern: "\\b" + MySttring + "\\b" If MyString is "one", this should pick up whole words like "one". The problem is, it will pick up also the word: "one.two"...
7
by: Matthew Warren | last post by:
Hi, I would expect this to work, rawstring=r'some things\new things\some other things\' But it fails as the last backslash escapes the single quote. ...although writing this I think I...
14
by: Andy B | last post by:
I need to create a regular expression that will match a 5 digit number, a space and then anything up to but not including the next closing html tag. Here is an example: <startTag>55555 any...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.