I'm looking for a RegEx to return all the characters between two square brackets, and only the characters, not the brackets themselves.
For example given a string that looks like "[A][B2][1,1][ABC]"
The RegEx should return: A
, B2
, 1,1
, ABC
I have tried various expressions for both String#split
and Pattern
& Match
.
The closest I've gotten was:
String.split("[\\[]*[\\]]");
and Pattern.compile("\\[[^\\]]+(?=])");
Both of these return: [A
, [B2
, [1,1
, [ABC
I've found other expressions that return the values with the enclosing brackets (ie. Pattern.compile("\\[[^\\]]*\\]");
) but that is not what I'm after.
Does someone know the correct expression? or is what I'm trying to do not possible?
(?<=\\[).*?(?=\\])
You need lookaheads
and lookbehinds
.or
(?<=\\[)[^\\]]*(?=\\])
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments