Help with Regex

M

Maqsood Ahmed

Hello,

I am trying to create a Regex object which can match ASCII character
0x05 in a given string. I have written following code to accomplish
this:

System.Text.RegularExpressions.Regex regex = new
System.Text.RegularExpressions.Regex("[\\x05]*?",
System.Text.RegularExpressions.RegexOptions.Compiled);

Now when I try to do like this:

regex.Matches(str);

It gives me an array of Match objects of length equal to the length of
the string. Even if the ASCII character is not present in the string.
Can anyone tell what am I getting wrong here?

Regards

Maqsood Ahmed
Kolachi Advanced Technologies
http://www.kolachi.net
 
J

Jesse Houwing

* Maqsood Ahmed wrote, On 7-7-2007 7:29:
Hello,

I am trying to create a Regex object which can match ASCII character
0x05 in a given string. I have written following code to accomplish
this:

System.Text.RegularExpressions.Regex regex = new
System.Text.RegularExpressions.Regex("[\\x05]*?",
System.Text.RegularExpressions.RegexOptions.Compiled);

Now when I try to do like this:

regex.Matches(str);

It gives me an array of Match objects of length equal to the length of
the string. Even if the ASCII character is not present in the string.
Can anyone tell what am I getting wrong here?

Regards

Maqsood Ahmed
Kolachi Advanced Technologies
http://www.kolachi.net


The * makes the whole regular expression optional. So at every character
in the string it matches nothing, but nothing, according to this
expression, is a match.

This expression should do the trick:

@"\x05"

Or if you want them grouped together if there is more than one instance
of \x05 you can use

@"\x05+"

I'm not sure what you're trying to accomplish, Regex is a very expensive
technique for finding an instance of a certain character in a string. A
Simple string.IndexOf should accomplish the same result, but much, much
faster.

Jesse
 
M

Maqsood Ahmed

* Maqsood Ahmed wrote, On 7-7-2007 7:29:




I am trying to create a Regex object which can match ASCII character
0x05 in a given string. I have written following code to accomplish
this:
System.Text.RegularExpressions.Regex regex = new
System.Text.RegularExpressions.Regex("[\\x05]*?",
System.Text.RegularExpressions.RegexOptions.Compiled);
Now when I try to do like this:

It gives me an array of Match objects of length equal to the length of
the string. Even if the ASCII character is not present in the string.
Can anyone tell what am I getting wrong here?

Maqsood Ahmed
Kolachi Advanced Technologies
http://www.kolachi.net

The * makes the whole regular expression optional. So at every character
in the string it matches nothing, but nothing, according to this
expression, is a match.

This expression should do the trick:

@"\x05"

Or if you want them grouped together if there is more than one instance
of \x05 you can use

@"\x05+"

I'm not sure what you're trying to accomplish, Regex is a very expensive
technique for finding an instance of a certain character in a string. A
Simple string.IndexOf should accomplish the same result, but much, much
faster.

Jesse- Hide quoted text -

- Show quoted text -

Yes Jesse, you are right, it was an expensive operation, I have done
it using string class.
Thanks for your help.

Regards,

Maqsood Ahmed
Kolachi Advanced Technologies
http://www.kolachi.net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top