tags:

views:

194

answers:

4

Hello guys,

i'm doing the simplest regex.match ever, i am giving the Regex.Match a pattern of one character and it returns no match at all, and i made sure the input text contains a lot of that character?

i checked all the usings.

its just very weird.

any help would be appreciated!

Thanks.

EDIT:

my sample is "doing any type of matching is simply not WORKING" returns an empty match

Match m=Regex.Match(@"c","abcdc");

the code is compiled with no errors, so why the NO MATCHING!!

+3  A: 

I assure you, the regular expression works. I have used it many, many times.

This will put the string "d" in the variable s:

string s = System.Text.RegularExpressions.Regex.Match("asdf", "d").Value;

If that doesn't work, perhaps you have some strange culture setting that affects how strings are compared? Does System.Globalization.CultureInfo.CurrentCulture.DisplayName return an expected value?

Guffa
+1 for the attempt to answer an incomplete question.
o.k.w
I just had a guess that the OP might be calling it like so: `Regex.Match("d", "adidsodsojdadadddd")`
Anon.
@Guffa thats exactly whats happening to me, i am giving it, only one character in the pattern that does exist in the input string.. ( i was just testing that it can match anything after many trials on my real problem )so i am gonna check that, and get back to you.Thanks man.
zanque
+9  A: 

EDIT: based on your edit the issue is that you're using the parameters out of order. You need to switch the order and supply the input (string source to find a match in) then the pattern (what to match against).

In fact, this order is specified for you by the IntelliSense as depicted in this image:

Regex parameters ordering

It usually helps to match the naming suggested by the IntelliSense or refer to it to ensure the proper items are being passed in.


What is the character being used? Chances are you're trying to use a character that is actually a metacharacter which holds special meaning in regex.

For example:

string result = Regex.Match("$500.00", "$").Value;

The above wouldn't return anything since $ is a metacharacter that needs to be escaped:

string result1 = Regex.Match("$500.00", @"\$").Value;  // or
string result2 = Regex.Match("$500.00", "\\$").Value;  // or
string result3 = Regex.Match("$500.00", Regex.Escape("$")).Value;

For a list of common metacharacters that need to be escaped look at the Regex.Escape documentation.

Ahmad Mageed
this is an amazing response to a not-very-good question.. kudos sir!
Jeff Atwood
@Jeff thank you for the kind words!
Ahmad Mageed
A: 

I recommend that you get RegExBuddy. That way you can check your regex's against a test file and make sure they should return something and that you didn't mess up your expression.

http://www.regexbuddy.com/

Blankasaurus
+3  A: 

You have the parameters in the wrong order in your example:

Match m=Regex.Match(@"c","abcdc");

This code means that you try to find the string "abcdc" in the string "c", try it the other way around and it should work better, ie:

Match m=Regex.Match("abcdc", "c");

Also, the fact that your code compiles doesn't mean that it will necessarily find a match...

Here is the documentation for Regex.Match.

Berg