Reputation: 21
I have found many answers which tell you how to extract the text inside square brackets as well as how to extract text outside of square brackets but none to do both.
I have a string: [Sometext]MoreText[SomeOtherText]
I am hoping to get [SomeText]
, MoreText
, [SomeOtherText]
.
Can this be done using a regex? Or am I better off looping over the string and extraction it that way? I need the order to be maintained.
Upvotes: 1
Views: 456
Reputation: 18357
You can use this regex, to match the data as per your sample in post,
\[(?:[^\]]*)\]|([a-zA-Z]+(?:\s+[a-zA-Z]+)*)
There are two sub-regex in it as alternation, where \[(?:[^\]]*)\]
will capture any text that will be of form [somedata]
and ([a-zA-Z]+(?:\s+[a-zA-Z]+)*)
regex will capture data of form somedata
or somedata somemoredata somemoredatafurther
Sample Java codes,
String s = "[Sometext]MoreText[SomeOtherText] I am hoping to get [SomeText], MoreText, [SomeOtherText]";
Pattern p = Pattern.compile("\\[(?:[^\\]]*)\\]|([a-zA-Z]+(?:\\s+[a-zA-Z]+)*)");
Matcher m = p.matcher(s);
while (m.find()) {
System.out.println(m.group());
}
Prints,
[Sometext]
MoreText
[SomeOtherText]
I am hoping to get
[SomeText]
MoreText
[SomeOtherText]
Upvotes: 1
Reputation: 124275
Assuming you will not have nested brackets you can try splitting
[
OR]
This can be done using look-around mechanisms.
String yourText = "[Sometext]MoreText[SomeOtherText]";
String[] arr = yourText.split("(?<=\\])|(?=\\[)");
Arrays.stream(arr).forEach(System.out::println);
Output:
[Sometext]
MoreText
[SomeOtherText]
Upvotes: 1