As the title says, I am trying to split bracketed text, for example "[ab][ij][yz]" into a list, like {"[ab]", "[ij]", "[yz]"} or {"ab", "ij", "yz"}. I looked at this question, and something similar would work, but the case there is more specific and I just need a simpler and more general regex.
CodePudding user response:
Here is one way to do so:
[^\[\]]
Each character different from [
or ]
is matched as many times as possible, the matches are therefore between each square brackets.
See the online demo here.
[^]
: Match a single character not present in the list.\[
: Matches[
.\]
: Matches]
.
In java:
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final Pattern pattern = Pattern.compile("[^\\[\\]] ");
final Matcher m = pattern.matcher("[ab][ij][yz]");
ArrayList<String> allMatches = new ArrayList<String>();
while (m.find()) {
allMatches.add(m.group(0));
}
System.out.println(allMatches);
}
}
CodePudding user response:
Example with the wrapping [
]
/\[[^\]] \]/g
console.log(
"[ab][ij][yz]".match(/\[[^\]] \]/g)
);
https://regex101.com/r/IljP6k/3
Example without the wrapping [
]
using Positive lookbehind (?<=)
/(?<=\[)[^\]] /g
console.log(
"[ab][ij][yz]".match(/(?<=\[)[^\]] /g)
);