473,404 Members | 2,137 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 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
Nov 16 '05 #1
2 1616
Hi Henry,
Regular expressions are made up of many of the characters that you placed
into your string. Including the [ and ] characters. The pattern that you
specify has an invalid part [] which is a pattern group where any member can
be matched, but it has no members. that makes the pattern invalid.

Moving the ] to the front of the string means that the regex parser will not
see the bad pattern. However to be truly correct, you still need to quote
any of the characters that can be interpreted as part of a pattern.

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

Hope this helps,
--- Nick

"Henry" <ig******@hotmail.com> wrote in message
news:%2****************@tk2msftngp13.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, "" );

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

Nov 16 '05 #2
Hi Henry,

in addition of Nick's answer, there is a little modification

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

Regards
Martin
"Nick Malik" <ni*******@hotmail.nospam.com> wrote in message
news:OcKFc.21331$Oq2.9279@attbi_s52...
Hi Henry,
Regular expressions are made up of many of the characters that you placed
into your string. Including the [ and ] characters. The pattern that you
specify has an invalid part [] which is a pattern group where any member can be matched, but it has no members. that makes the pattern invalid.

Moving the ] to the front of the string means that the regex parser will not see the bad pattern. However to be truly correct, you still need to quote
any of the characters that can be interpreted as part of a pattern.

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

Hope this helps,
--- Nick

"Henry" <ig******@hotmail.com> wrote in message
news:%2****************@tk2msftngp13.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, "" );

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


Nov 16 '05 #3

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...
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),...
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...
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...
1
by: Allan Ebdrup | last post by:
I have a dynamic list of regular expressions, the expressions don't change very often but they can change. And I have a single string that I want to match the regular expressions against and find...
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.