Optional pattern separator in regular expression

I want to create a template that will match a given string and create multiple groups from that string.

Input string:

Case 1: wp/video/video123/xyz/abc
Case 2: wp/video/video123
Case 3: wp/video
Case 4: wp

      

And the desired output:

Case1: group1=wp,group2=video,group3=video123
Case2: group1=wp,group2=video,group3=video123
Case3: group1=wp,group2=video
Case4: group1=wp

      

The template I created matches the first two cases, but ignores the last two cases:

(.*?)/+(.*?)/(.*?)[/.]

      

+3


source to share


1 answer


You can do what you want without regex using pure String.Split

and then group access:

String input ="wp/video/video123/xyz/abc";
String[] spts = input.split("/");
System.out.println("group1=" + spts[0] + ";group2=" + spts[1] + ";group3=" + spts[2]);

      

This will output group1=wp;group2=video;group3=video123

(see demo ).

If you need a regex, nhahtdh has already provided you with a sample regex that uses optional non-capturing groups, but I also suggest using all s .*

at the end of the pattern, or we're going to get additional matches with xyz/abc

:



String str = "wp/video/video123/xyz/abc";
String rx = "([^/]+)(?:/([^/]+)(?:/([^/]+)?)?)?.*";
Pattern ptrn = Pattern.compile(rx);
Matcher m = ptrn.matcher(str);
while (m.find()) {
    System.out.println("group1=" + m.group(1) + ",group2=" + m.group(2) + ",group3=" + m.group(3));
}

      

Regex explanation:

  • ([^/]+)

    - 1st group of 1 or more characters other than /

  • (?:/([^/]+)(?:/([^/]+)?)?)?

    - an additional capture group that matches
    • /

      - literal /

    • ([^/]+)

      - the second group of 1 or more characters other than /

    • (?:/([^/]+)?)?

      - an additional capture group that matches the same content as described above
  • .*

    - match all characters except newline to the end of the line so that we don't get any further matches. Delete it if you want to continue the matches. Or replace with (?=\\s|$)

    look-ahead to match before space or end of line.

See another demo here

+1


source







All Articles