Home > Mobile >  Regex for splitting bracketed text "[ab][ij][yz]" into a list of ["[ab]", "
Regex for splitting bracketed text "[ab][ij][yz]" into a list of ["[ab]", "

Time:03-15

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 ].
  • : 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);
    }
}

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)
);

https://regex101.com/r/IljP6k/2

  • Related