By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,813 Members | 1,257 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,813 IT Pros & Developers. It's quick & easy.

any regex gurus out there?

P: n/a

I'd appreciate it if you could advise.

1. How do I replace "\" (backslash) with anything?

2. Suppose I want to replace

(a) every occurrence of characters "a", "b", "c", "d" with "x",
(b) every occurrence of characters "p", "q", "r", "s" with "y".

Right now, I do it as follows:

myString = Regex.Replace( myString, "[abcd]", "x" );
myString = Regex.Replace( myString, "[pqrs]", "y" );

Is there a better way? Can the replacing be performed in one fell swoop?

Nov 16 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
"bill tie" <bi*****@discussions.microsoft.com> wrote in
news:7F**********************************@microsof t.com...

I'd appreciate it if you could advise.

1. How do I replace "\" (backslash) with anything?
Do you mean like this:
myString = Regex.Replace( myString, @"\\", "anything" );
2. Suppose I want to replace

(a) every occurrence of characters "a", "b", "c", "d" with "x",
(b) every occurrence of characters "p", "q", "r", "s" with "y".

Right now, I do it as follows:

myString = Regex.Replace( myString, "[abcd]", "x" );
myString = Regex.Replace( myString, "[pqrs]", "y" );

Is there a better way? Can the replacing be performed in one fell swoop?


You can do that with a MatchEvaluator, like this:

using System;
using System.Text.RegularExpressions;

class Test
{
static void Main()
{
string myString = "abcdefgpqrst";
myString = Regex.Replace( myString, "([abcd])|[pqrs]", new
MatchEvaluator(MyEvaluator));
Console.WriteLine(myString);
}

static string MyEvaluator(Match m)
{
if (m.Groups[1].Length != 0)
return "x";
else
return "y";
}
}

However, I personally would prefer the 2-pass version for a simple problem
like this.

Niki
Nov 16 '05 #2

P: n/a
"bill tie" <bi*****@discussions.microsoft.com> wrote in
news:D5**********************************@microsof t.com...

Niki,

Thank you. Your examples were useful.

1. I would never know about the mysterious at-sign. Searching for it in
1GB
of the MSDN Library on my desktop was utterly futile. Only after I
consulted
Jesse Liberty's book, did I find VERBATIM.

But this verbatim may actually raise another problem:

I'm reading an input string entered by a user in the browser, and shaving
undesired characters such as a backslash. If I "make" the string
verbatim,
"Verbatimness" only applies to string literals: "\\n" is exactly the same
string constant as @"\n" - it's just that in the first case, the compiler
treats the backslash as an escape character. If you read strings from some
other source you can think of any string as "verbatim".
I'm afraid -- I haven't tried yet -- I won't be able to remove whitespace
(
\n \r \t ) easily like so:

myString = Regex.Replace( myString, "[\n\r\t]+", "" ); You should make the search pattern verbatim, like this:
myString = Regex.Replace( myString, @"[\n\r\t]+", "" );
I haven't tried either, but I'm pretty sure this should work.

The backslash is normally used by the regex for regex metacharacters like \w
or \d. If you want to have a 'real' backslash in your pattern, you have to
escape it - with a backslash (same as in C strings). If you use a string
constant as a pattern, use something like @"\\", or \\\\. If you need to
escape a whole string, you can also use Regex.Escape, which escapes all
metacharacters in a string with backslashes. So: Regex.Match(someString,
Regex.Escape("[a-z]\n")) matches for (literally) "[a-z]\n, i.e. a opening
bracket, followed by an 'a', followed by a dash...
2. You proposed a matching algorithm. I have fifteen patterns to search.
The matching algorithm seems more efficient than making fifteen passes
over a
string of 100 or so characters.
As usual: Trust noone but your own benchmark if performance is important.
Having said that: The problem with the MatchEvaluator-technique is that
calling delegates is
quite slow, so 15 compiled regex's might well be faster than one
MatchEvaluator-Pass.
And: for 100 character strings I'd definitely favour readability over
performance. The time it takes to process such short strings will hardly be
measurable.
Let me make sure I understand. Suppose I'd like to replace

[abcd] with "z"
[pqrs] with "y"
[123] with "x"

Is this what it should look like?

string myString = "abcd efg pqrs t 1 k 2 m 3 n";
myString = Regex.Replace( myString, "(([abcd])|[pqrs])|[123]",
new MatchEvaluator(MyEvaluator));

static string MyEvaluator(Match m)
{
if (m.Groups[2].Length != 0)
return "z";
if (m.Groups[1].Length != 0)
return "y";
if (m.Groups[0].Length != 0)
return "x";

return null;
}
Yes, exactly.
If I were undaunted by the growing number of parentheses in

"(([abcd])|[pqrs])|[123]"

is there a way to obtain the index of the Groups collection?

Could I do something like

if ( m.Groups[i].Length != 0 )
{
switch (i)
{
case 0: ...
}
}


You could of course use a for loop over all groups. Is that what you meant?

Niki
Nov 16 '05 #3

P: n/a

Niki,

Thank you for good explanations.

[1]
If you want to have a 'real' backslash in your pattern,
you have to escape it - with a backslash
This is precisely what I couldn't achieve without the at-sign.

myString = "foo & ! ( \ * bar";
// after we clean up we should have only "foo spaces bar"
myString = Regex.Replace( myString, "[\\\!&*(]", "" );

No matter what number or combination of backslashes I used it didn't work.

The point of this exercise is I want to remove everything except:
letters a through z, A through Z
numerals 0 through 9
hyphen -
space " "

I thought I could do it by negation as follows:

myString = Regex.Replace( myString, "[^0-9a-zA-Z ]", "" );

This would be fantastic. There seem to be two problems:

(a) Regex considers some characters, e.g. parentheses, as letters.

{b) Letters the user enters may not be ordinary English letters. In the
French language, the letter A may have three different accents. I'm not sure
how the pattern [a-z] treats these letters.

On this point, do you have any suggestions?

[2] The problem with the MatchEvaluator-technique is that
calling delegates is quite slow, so 15 compiled regex's
might well be faster than one MatchEvaluator-Pass.
I was wondering, too. Yet, some compilers optimize code. I'm terribly new
to C#. I don't know whether it collapses my "calls" into some sort of loop
at compile-time.
I'd definitely favour readability over performance.
I concur the readability of the matching algorithm is awful.

Albeit I'm more inclined to apply the classic technique I started with, I'd
like to complete this intellectual exercise. Understanding how matching and
grouping work may be useful in other situations.

Using my last example:

Replace

[abcd] with "z"
[pqrs] with "y"
[123] with "x"

string myString = "abcd efg pqrs t 1 k 2 m 3 n";
myString = Regex.Replace( myString, "(([abcd])|[pqrs])|[123]",
new MatchEvaluator(MyEvaluator));

static string MyEvaluator(Match m)
{
if (m.Groups[2].Length != 0)
return "z";
if (m.Groups[1].Length != 0)
return "y";
if (m.Groups[0].Length != 0)
return "x";

return null;
}
is there a way to obtain the index of the Groups collection?

Could I do something like

if ( m.Groups[i].Length != 0 )
{
switch (i)
{
case 0: ...
}
}

You could of course use a for loop over all groups. Is that
what you meant?


I'm not sure what I meant. ;-)

Slavishly, I tried to apply examples I found in the MSDN Library.

My code looked something like this:

static string MyEvaluator(Match m)
{
for ( int i = 0; i < m.Groups.Count; i++ )
{
if (m.Groups[i].Length != 0)
{
switch (i)
{
case 0:
return "x";
case 1:
return "y";
case 2:
return "z";
}
}
return null;
}
return null;
}

This didn't do the job I wanted.

Any ideas?

Thank you.

Nov 16 '05 #4

P: n/a
"bill tie" <bi*****@discussions.microsoft.com> wrote in
news:DD**********************************@microsof t.com...
...
myString = "foo & ! ( \ * bar";
// after we clean up we should have only "foo spaces bar"
myString = Regex.Replace( myString, "[\\\!&*(]", "" );

No matter what number or combination of backslashes I used it didn't work.
I assume the first string constant really is myString = "foo & ! ( \\ *
bar"; - The compiler wouldn't take it otherwise.

The pattern you want to pass to the regex class looks like this: [\\!&*(]
You can either write it this way:
myString = Regex.Replace( myString, @"[\\!&*(]", "" );
or escape each backslash with a backslash, like this:

myString = Regex.Replace( myString, "[\\\\!&*(]", "" );
Note that this is exactly the same code, it's just a different way to write
the string constant.
Also, this doesn't have to do anything with regular expressions: If you want
to code a UNC-path in your code like \\SomeMachine\SomeFolder, you'd either
write @"\\SomeMachine\SomeFolder", or "\\\\SomeMachine\\SomeFolder".
The point of this exercise is I want to remove everything except:
letters a through z, A through Z
numerals 0 through 9
hyphen -
space " "

I thought I could do it by negation as follows:

myString = Regex.Replace( myString, "[^0-9a-zA-Z ]", "" );

This would be fantastic. There seem to be two problems:

(a) Regex considers some characters, e.g. parentheses, as letters.
I don't think so. Maybe you have a sample?
{b) Letters the user enters may not be ordinary English letters. In the
French language, the letter A may have three different accents. I'm not
sure
how the pattern [a-z] treats these letters.
They're not included in this set. You'll have to use unicode character
classes for that problem:
myString = Regex.Replace( myString, @"[^\p{Ll}\p{Lu}\p{Lt}\p{Lo}\p{Nd} ]",
"" );
(As usual, if you want a non-verbatim string, replace all \ characters with
\\)
\p matches for a unicode character class:
\p{Ll}matches for "Letter lowercase", no matter what language
\p{Lu}matches for "Letter uppercase", no matter what language
\p{Lt}matches for "Letter titlecase", no matter what language
\p{Lo}matches for "other Letters", no matter what language
\p{Nd}matches for "Numeric decimal"

A list of all unicode character classes can be found here:
http://msdn.microsoft.com/library/de...classtopic.asp

Note that if you want to include underscores in this set you can use the
shortcut '\w' resp. '\W'.

myString = Regex.Replace( myString, @"[^\w ]", "" );
[2]
The problem with the MatchEvaluator-technique is that
calling delegates is quite slow, so 15 compiled regex's
might well be faster than one MatchEvaluator-Pass.
I was wondering, too. Yet, some compilers optimize code. I'm terribly
new
to C#. I don't know whether it collapses my "calls" into some sort of
loop
at compile-time.


There's a good article on .NET optimizations:
http://msdn.microsoft.com/library/de...anagedcode.asp
As you can see there, delegates are a lot slower than usual function calls.
I'd definitely favour readability over performance.


I concur the readability of the matching algorithm is awful.


Only if you don't know regular expressions. (And that probably applies to
every kind of code)
To use one of your previous examples:
myString = Regex.Replace( myString, "[^0-9a-zA-Z ]", "" );
I think readability of this line is great: Think about what the
corresponding C# code would look like; I'm pretty sure it would be a lot
more complex, and harder to read.
And you can always use tools like Expresso that make work with regex's
really easy.

So, I would disagree in that point.
Albeit I'm more inclined to apply the classic technique I started with,
I'd
like to complete this intellectual exercise. Understanding how matching
and
grouping work may be useful in other situations.

Using my last example:

Replace

[abcd] with "z"
[pqrs] with "y"
[123] with "x"

string myString = "abcd efg pqrs t 1 k 2 m 3 n";
myString = Regex.Replace( myString, "(([abcd])|[pqrs])|[123]",
new MatchEvaluator(MyEvaluator));
Paranthesis is wrong in this pattern. Use this one:
myString = Regex.Replace( myString, "([abcd])|([pqrs])|([123])",
new MatchEvaluator(MyEvaluator));
...
My code looked something like this:

static string MyEvaluator(Match m)
{
for ( int i = 0; i < m.Groups.Count; i++ )
{
if (m.Groups[i].Length != 0)
{
switch (i)
{
case 0:
return "x";
case 1:
return "y";
case 2:
return "z";
}
}
return null;
}
return null;
}

This didn't do the job I wanted.


Yes, there's a tweak here: Groups[0] always refers to the whole capture;
Groups[1] returns the contents of the first paranthesis, and so on.
Try this one:

static string MyEvaluator(Match m)
{
for ( int i = 1; i < m.Groups.Count; i++ )
{
if (m.Groups[i].Length != 0)
{
switch (i)
{
case 1:
return "x";
case 2:
return "y";
case 3:
return "z";
}
}
}
return null;
}
Niki
Nov 16 '05 #5

P: n/a
"bill tie" <bi*****@discussions.microsoft.com> wrote in
news:43**********************************@microsof t.com...
...
I think I'm getting where I want to be. There seems to be one last thing.

I need to replace accented letters with ordinary ASCII letters. In pseudo
code, it looks as follows:

replace ( myString, "[various and devious a's]", "a" )

In the "[various and devious a's]" part I pasted letters I had copied from
a
source on the web. I saved and closed my file. After I re-opened the
file
some a's lost their accents.

What do you suggest I do?


Usually .cs files are stored as ASCII files; I never tried this, but I think
you can tell Visual Studio to save your file in unicode format via File -
Advanced Save Options. If this doesn't work, you can still copy your
character into window's character table and use it's hex representation in
code like '\x0041' for 'A'.

Niki
Nov 16 '05 #6

P: n/a

Niki,

Thank you. Your examples were useful.

1. I would never know about the mysterious at-sign. Searching for it in 1GB
of the MSDN Library on my desktop was utterly futile. Only after I consulted
Jesse Liberty's book, did I find VERBATIM.

But this verbatim may actually raise another problem:

I'm reading an input string entered by a user in the browser, and shaving
undesired characters such as a backslash. If I "make" the string verbatim,
I'm afraid -- I haven't tried yet -- I won't be able to remove whitespace (
\n \r \t ) easily like so:

myString = Regex.Replace( myString, "[\n\r\t]+", "" );

2. You proposed a matching algorithm. I have fifteen patterns to search.
The matching algorithm seems more efficient than making fifteen passes over a
string of 100 or so characters.

Let me make sure I understand. Suppose I'd like to replace

[abcd] with "z"
[pqrs] with "y"
[123] with "x"

Is this what it should look like?

string myString = "abcd efg pqrs t 1 k 2 m 3 n";
myString = Regex.Replace( myString, "(([abcd])|[pqrs])|[123]",
new MatchEvaluator(MyEvaluator));

static string MyEvaluator(Match m)
{
if (m.Groups[2].Length != 0)
return "z";
if (m.Groups[1].Length != 0)
return "y";
if (m.Groups[0].Length != 0)
return "x";

return null;
}

If I were undaunted by the growing number of parentheses in

"(([abcd])|[pqrs])|[123]"

is there a way to obtain the index of the Groups collection?

Could I do something like

if ( m.Groups[i].Length != 0 )
{
switch (i)
{
case 0: ...
}
}
Thank you again.

Nov 16 '05 #7

P: n/a

Niki,

Thank you for your reply.

[1]
(a) Regex considers some characters, e.g. parentheses, as letters.
I don't think so. Maybe you have a sample?


I stand corrected albeit I would've sworn I saw it. I ran a few new tests.
French language, the letter A may have three different accents. I'm not
sure
how the pattern [a-z] treats these letters.


They're not included in this set. You'll have to use unicode character
classes for that problem:
myString = Regex.Replace( myString, @"[^\p{Ll}\p{Lu}\p{Lt}\p{Lo}\p{Nd} ]",
"" );


It's a good thing you wrote this. I checked "Character Classes" in the MSDN
Library. Lo and behold, my problem is solved.

myString = Regex.Replace( myString, "[^\\w\\d ]", "" );

removes everything but underscores. Nice, very nice.

[2] Paranthesis is wrong in this pattern. Use this one:
myString = Regex.Replace( myString, "([abcd])|([pqrs])|([123])",
new MatchEvaluator(MyEvaluator));


This

"([abcd])|([pqrs])|([123])"

is much easier on the eyes than my

"(([abcd])|[pqrs])|[123]"

[3]
I think I'm getting where I want to be. There seems to be one last thing.

I need to replace accented letters with ordinary ASCII letters. In pseudo
code, it looks as follows:

replace ( myString, "[various and devious a's]", "a" )

In the "[various and devious a's]" part I pasted letters I had copied from a
source on the web. I saved and closed my file. After I re-opened the file
some a's lost their accents.

What do you suggest I do?

Nov 16 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.