C # regex matching

18.jun. 7 noči od 515,00 EUR

here I would like to get 515.00 with the expression regurar.

Regex regularExpr = new Regex(@rule.RegularExpression,
                                                          RegexOptions.Compiled | RegexOptions.Multiline |
                                                          RegexOptions.IgnoreCase | RegexOptions.Singleline |
                                                          RegexOptions.IgnorePatternWhitespace);

tagValue.Value = "18.jun. 7 noči od 515,00 EUR";
Match match = regularExpr.Match(tagValue.Value);

object value = match.Groups[2].Value;

regex: \d+((.\d+)+(,\d+)?)?

but I always get a "". If I try this regular expression in Expresso, I get an array of 3 values, and the third - 515.00

what is wrong in c # code that i get an empty string

+3
source share
3 answers

Your regular expression matches 18(since decimal parts are optional), and match.Groups[2]refers to the second bracket (.\d+), which should read correctly (\.\d+)and not participate in the match, so an empty line is returned.

You need to fix your regular expression and iterate over the results:

StringCollection resultList = new StringCollection();
Regex regexObj = new Regex(@"\d+(?:[.,]\d+)?");
Match matchResult = regexObj.Match(subjectString);
while (matchResult.Success) {
    resultList.Add(matchResult.Value);
    matchResult = matchResult.NextMatch();
} 

resultList[2] will contain your match.

+4

, .

Regex re = new Regex("\d+((.\d+)+(,\d+)?)?")

Regex re = new Regex(@"\d+((.\d+)+(,\d+)?)?")

, .

+4

, , Expresso, :

string s = "18.jun. 7 noči od 515,00 EUR";
Regex r = new Regex(@"\d+((.\d+)+(,\d+)?)?");
foreach (Match m in r.Matches(s))
{
  Console.WriteLine(m.Value);
}

, , , . :

Console.WriteLine("{0,10} {1,10} {2,10} {3,10}",
  @"Group 0", @"Group 1", @"Groups 2", @"Group 3");
Regex r = new Regex(@"\d+((.\d+)+(,\d+)?)?");
foreach (Match m in r.Matches(s))
{
  Console.WriteLine("{0,10} {1,10} {2,10} {3,10}",
    m.Groups[0].Value, m.Groups[1].Value, m.Groups[2].Value, m.Groups[3].Value);
}

:

Group 0    Group 1    Group 2    Group 3
     18
      7
 515,00        ,00        ,00

. , , . , , ,00 , :

@"(?n)\b\d+(\.\d+)*(,\d+)\b"

(?n) - ExplicitCapture, , . RegexOptions , - , - Compiled, , hogging. \b - .

It looks like you are applying all of these modifiers blindly to each regular expression when you create them, which is not very good. If a particular regular expression needs a specific modifier, you should try to specify it in the regular expression itself using the built-in modifier, as I did with (?n).

+2
source

All Articles