简体   繁体   中英

Regex to extract the string both inside and outside a square bracket

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.

Assuming you will not have nested brackets you can try splitting

  • before [ OR
  • after ]

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]

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

Demo

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM