Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression with & as separator

Tags:

java

regex

I was given a long text in which I need to find all the text that are embedded in a pair of & (For example, in a text "&hello&&bye&", I need to find the words "hello" and "bye").

I try using the regex ".*&([^&])*&.*" but it doesn't work, I don't know what's wrong with that.

Any help?

Thanks

like image 481
jackyokboy Avatar asked Dec 27 '22 04:12

jackyokboy


1 Answers

Try this way

String data = "&hello&&bye&";
Matcher m = Pattern.compile("&([^&]*)&").matcher(data);
while (m.find())
    System.out.println(m.group(1));

output:

hello
bye
like image 140
Pshemo Avatar answered Jan 10 '23 13:01

Pshemo