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 5 2276
"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
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
"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
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!
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
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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),...
|
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 & for &. CDATA does not seem...
|
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"...
|
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...
|
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...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
|
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,...
| |