Regex with optional matching groups

nan

I'm trying to parse given string which is kind a of path separated with /. I need to write regex that would match each segment in the path to corresponding regex group.

Example 1:

input:

/EAN/SomeBrand/appliances/refrigerators/RF444

output:

Group: producer, Value: SomeBrand Group: category, Value: appliances Group: subcategory, Value: refrigerators Group: product, Value: RF4441

Example 2:

input:

/EAN/SomeBrand/appliances

output:

Group: producer, Value: SomeBrand Group: category, Value: appliances Group: subcategory, Value: Group: product, Value:

I tried following code, it works fine when the path is full (like in the first exmaple) but fails to find the groups when the input string is impartial (like in example 2).

static void Main()
{
  var pattern = @"^" + @"/EAN"
                + @"/" + @"(?<producer>.+)"
                + @"/" + @"(?<category>.+)"
                + @"/" + @"(?<subcategory>.+)"
                + @"/" + @"(?<product>.+)?"
                + @"$";

  var rgx = new Regex(pattern, RegexOptions.Compiled | RegexOptions.IgnoreCase);
  var result = rgx.Match(@"/EAN/SomeBrand/appliances/refrigerators/RF444");

  foreach (string groupName in rgx.GetGroupNames())
  {
    Console.WriteLine(
       "Group: {0}, Value: {1}",
       groupName,
       result.Groups[groupName].Value);
  }


  Console.ReadLine();
}

Any suggestion is welcome. Unfortunately I cannot simply split the string since the framework I'm using expects regex object.

xanatos

Try

var pattern = @"^" + @"/EAN"
    + @"(?:/" + @"(?<producer>[^/]+))?"
    + @"(?:/" + @"(?<category>[^/]+))?"
    + @"(?:/" + @"(?<subcategory>[^/]+))?"
    + @"(?:/" + @"(?<product>[^/]+))?";

Note how I replaced the . with [^/], because you want to use the / to split strings. Note even the use of the optional quantifier for each sub-part (?)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Regex - groups matching same pattern

From Dev

Complex Regex Matching with Capture Groups

From Dev

Matching optional string in regex

From Dev

Matching with Regex in Javascript (Optional substring)

From Dev

Java, Regex, Nested optional groups

From Dev

Regex Matching optional numbers

From Dev

Regex replace only matching groups and ignore non-matching groups?

From Dev

Python Regex Skipping Optional Groups

From Dev

Regex: matching unknown groups that repeat?

From Dev

Java Regex to Parse a Path Into Multiple Optional Groups

From Dev

Regex: Capturing optional beginning or ending groups/subgroups

From Dev

regex - matching number with optional range

From Dev

Java Regex Matching Groups Bug?

From Dev

regex matching and all optional groups

From Dev

regex with optional capturing groups and negative lookahead

From Dev

Python Regex with all optional groups

From Dev

Regex Matching of Optional Character in PHP

From Dev

Complex Regex Matching with Capture Groups

From Dev

javascript regex: n-times optional groups

From Dev

Python regex not matching multiple groups

From Dev

Java, Regex, Nested optional groups

From Dev

python regex multiple optional capture groups

From Dev

Regex optional matching

From Dev

Javascript regex with optional capture groups - matching a mongo query

From Dev

Regex optional with two matching groups

From Dev

Extracting matching groups with Scala regex

From Dev

Regex is not matching all alternate groups

From Dev

Optional groups in regex

From Dev

python regex optional named groups