Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split using multiple keywords as demiliters

Tags:

java

split

This question answers how we can split a string using multiple delimiters? Is it also possible to split a String using multiple keywords.

For example:

String myString = (One Two Three) AND (Four Five Six) OR (Six Seven Eight)

And I want to split myString at both AND and OR so that I get:

  someArray[0] = (One Two Three)

  someArray[1] = (Four Five Six)

  someArray[2] = (Six Seven Eight)

I do not know much of regex, so any help or advice would be helpful. Thanks in advance.

like image 467
Anjan Baradwaj Avatar asked Jan 30 '26 15:01

Anjan Baradwaj


1 Answers

Try this :

String[] someArray=yourString.split("(AND)|(OR)");
like image 65
Mifmif Avatar answered Feb 02 '26 06:02

Mifmif



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!