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
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…