'Regex for splitting bracketed text "[ab][ij][yz]" into a list of ["[ab]", "[ij]", "[yz]"]
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.
Solution 1:[1]
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]
.
+
: Matches the previous token between one and unlimited times, as many times as possible.
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);
}
}
Solution 2:[2]
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)
);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Cubix48 |
Solution 2 |