Suzan
Suzan

Reputation: 305

Substring string with regex before first match in Java

Is it possible to substring string with regex before first match in Java?

String str = "&&param1=value 1&&param1=value&2&&param1=value & 2&param2=aaas&param3=99&param4=bbb";

I want to have a result like this:

&&param1=value 1&&param1=value&2&&param1=value & 2

Upvotes: 2

Views: 145

Answers (2)

If you simply want to split the string with spaces, you can try the following code

String str = "&&param1=value 1&&param1=value&2&&param1=value & 2&param2=aaas&param3=99&param4=bbb";
String[] strAray = str.split(" ");//strArray contains all the splitted tokens
for(String s : strAray){
    System.out.println(s);//Prints out each token
}

Upvotes: 0

anubhava
anubhava

Reputation: 784958

You can use this regex for matching your test:

^.*?(?=(?<!&)&\w+=)

RegEx Demo

In Java this regex will be:

"^.*?(?=(?<!&)&\\w+=)"

Upvotes: 1

Related Questions