简体   繁体   中英

java: Extract a substring using regular expression

I have String data in which I am interested to extract a substring but I am stuck on creating the regex pattern for that.The String data I have is following:

$.ajax({url:"Q" + "uestions?" 
                + "" + "action=" 
                + "maxim" + "um&" 
                + "p043366329446409=08315891235072667&" 
                + "c" + "ity=" 
                + k.val() + "&" 
                + e + "=888",success:succFun,error:errFun,async:false});
        };

I want to extract p043366329446409=08315891235072667 part from the above string.This data changes everytime I make request to server but "p0" will always start the string and &" will end the string.

Thanks EveryOne.

尝试这个

String p0 = s.replaceAll(".*&(p0.+?=.+?)&.*", "$1");

Try this one:

String mydata = "<query string>";
Pattern pattern = Pattern.compile("p0([0-9]+)=([0-9]+)&");  

Matcher matcher = pattern.matcher(mydata);
int start=0,end=0; 
if(matcher.find())
{
   start=matcher.start();
   end=matcher.end();

   System.out.println(mydata.substring(start,end-1));
}

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