-2

This Regular expression Eorks on test site (e.g. regex101.com). But C# return null. Why is that?

static public List <string> GetAllOccurenceByRegExp (string original, string regExp) {

    List<string> matchList = new List<string> ();
    try {
        matchList = Regex.Matches (original, regExp)
                        .Cast<Match> ()
                        .Select (m => m.Groups[0].Value)
                        .ToList ();
    }
    catch (Exception e) {
    }
    return matchList;
}

Nothing match:

string pcsPlusWeightRegEx = @"[\d]{1,10}(\s)?(шт|щт|бут|\*)?(\.|\,)?+(\s)?[x|х](\s)?[\d]{1,10}(\s)?((г|гр|грамм)|(кг|киллограмм)|(мл|миллилитров)|(л|литров|литра|литр))";
string name = "Сок яблочно-грушевый ФрутоНяня, с мякотью, с 3 лет, 15 шт х 500 мл";
string pcsPlusWeightMatch = StringChange.GetAllOccurenceByRegExp (name, pcsPlusWeightRegEx).FirstOrDefault (); //return null!

Other Reg Expressions in c# works well. This is WPF.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
thedriveee
  • 609
  • 3
  • 9
  • 22
  • 1
    Try removing the nested quantifier `+`: `[\d]{1,10}(\s)?(шт|щт|бут|\*)?(\.|\,)?(\s)?[x|х](\s)?[\d]{1,10}(\s)?((г|гр|грамм)|(кг|киллограмм)|(мл|миллилитров)|(л|литров|литра|литр))` – Rahul Sharma Jan 07 '20 at 07:19

1 Answers1

1

Regarding your case, you would have to remove the nested quantifier +:

[\d]{1,10}(\s)?(шт|щт|бут|\*)?(\.|\,)?(\s)?[x|х](\s)?[\d]{1,10}(\s)?((г|гр|грамм)|(кг|киллограмм)|(мл|миллилитров)|(л|литров|литра|литр))

More information on this here

Rahul Sharma
  • 7,768
  • 2
  • 28
  • 54