开发者

C# regular expression engine does not work [closed]

开发者 https://www.devze.com 2022-12-16 16:10 出处:网络
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time,or an extraordinarily narrow situation that is not generally applic
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide aud开发者_StackOverflowience of the internet. For help making this question more broadly applicable, visit the help center. Closed 10 years ago.

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!!


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:

C# regular expression engine does not work [closed]

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.


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.


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?

0

精彩评论

暂无评论...
验证码 换一张
取 消