Question

I have currently some URL like this :

?param=value&offset=19&size=100

Or like that :

?offset=45&size=50&param=lol

And I would like to remove for each case the "offset" and the "value". I'm using the regex method but I don't understand how it's really working... Can you please help me for that? I also want to get both values of the offset and the size.

Here is my work...

\(?|&)offset=([0-9])[*]&size=([0-9])[*]\

But it doesn't works at all!

Thanks.

Était-ce utile?

La solution

Assuming is Javascript & you only want to remove offset param:

str.replace(\offset=[0-9]*&?\,"")

For Java:

str=str.replaceAll("offset=[0-9]*&?","");
//to remove & and ? at the end in some cases
if (str.endsWith("?") || str.endsWith("&")) 
    str=str.substring(0,str.length()-1);

Autres conseils

With out regex .

String queryString ="param=value&offset=19&size=100";
    String[] splitters = queryString.split("&");
    for (String str : splitters) {
        if (str.startsWith("offset")) {
            String offset = str.substring(str.indexOf('=') + 1);//like wise size also
            System.out.println(offset); //prints 19
        }
    } 

If you need to use a regular expression for this then try this string in java for the regular expression (replace with nothing):

"(?>(?<=\\?)|&)(?>value|offset)=.*?(?>(?=&)|$)"

It will remove any parameter in your URL that has the name 'offset' or 'value'. It will also conserve any required parameter tokens for other parameters in the URL.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top