Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression, match substring between pipes

Tags:

regex

I want to extract/match substrings/sizes in the following string "|XS|XL|S|M|" using regular expression. In this particular case, XS, XL, S and M.

I have tried the following regular expressions without success.

\|(\w+)\|

Matches: XS, S

(?=.(\w+)) 

Matches: XS, S, XL, L, S, M

like image 573
user634545 Avatar asked Oct 26 '25 07:10

user634545


1 Answers

You problem with the first match is that is consumes the pipes, so they are not there for the next match.

The second pattern is a little convoluted but what you are saying is for each character in the string grab all word characters that follow it, without consuming them. So at the first pipe that is XS, the engine then moves to the X where the answer is S. The engine then moved to the S where the pattern doesn't match.

You need to use positive lookaround, so you match and consume the text between pipes without consuming the pipes. You want to, for any group of word characters, assert that it has a pipe preceding and following it. In which case, you want to consume it.

If your language supports it (You don't mention which regex engine you are using) this pattern will work:

(?<=\|)[^|]++(?=\|)
  • (?<=\|) asserts that there is a pipe behind the pattern
  • [^|]++ possessively matches all non-pipe characters
  • (?=\|) asserts that there is a pipe following the pattern

Here is a testcase in Java (ignore the \\, there are just Java syntax):

public static void main(String[] args) throws Exception {
    final String test = "|XS|XL|S|M|";
    final Pattern pattern = Pattern.compile("(?<=\\|)[^|]++(?=\\|)");
    final Matcher matcher = pattern.matcher(test);
    while(matcher.find()) {
        System.out.println(matcher.group());
    }
}

Output:

XS
XL
S
M
like image 137
Boris the Spider Avatar answered Oct 28 '25 00:10

Boris the Spider



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!