1

I have a query string that could be:

/fr/hello?language=en

or

/fr/welcome?param1=222&param2=aloa&language=en

or

/it/welcome?param1=222&language=en&param2=aa

I would like to remove from each query string the parameter language with its value, therefore the results would be:

/fr/hello

and

/fr/welcome?param1=222&param2=aloa

and

/it/welcome?param1=222&param2=aa

EDIT: The length of the value of the parameter could be more than 2

Does anybody know any good regex expression to use in String.replaceAll([regex],[replace]) ?

2
  • no, it doesn't work in my case, obviously I have replace 'foo' with 'language' Commented Jul 16, 2014 at 11:52
  • strangely it doesn't match Commented Jul 16, 2014 at 11:53

6 Answers 6

5

Use the below regex and replace the matched strings with empty string,

[&?]language.*?(?=&|\?|$)

DEMO

Example code:

String s1 = "/fr/welcome?param1=222&param2=aloa&language=en";
String s2 = "/fr/welcome?language=en";
String s3 = "/fr/welcome?param1=222&language=en&param2=aa";
String m1 = s1.replaceAll("[&?]language.*?(?=&|\\?|$)", "");
String m2 = s2.replaceAll("[&?]language.*?(?=&|\\?|$)", "");
String m3 = s3.replaceAll("[&?]language.*?(?=&|\\?|$)", "");
System.out.println(m1);
System.out.println(m2);
System.out.println(m3);

Output:

/fr/welcome?param1=222&param2=aloa
/fr/welcome
/fr/welcome?param1=222&param2=aa

IDEONE 1 or IDEONE 2

Sign up to request clarification or add additional context in comments.

8 Comments

I can't understand why I doesn't match in my code: queryString.replaceAll("[&?]language.*?(?=&|\\?|$)", "");
but it does not work for this: /en/welcome?language=en
@daniele now see the ideone link.
Does not work for /it/welcome?language=en&param1=222 which ends up as /it/welcome&param1=222 (query does not start with ? anymore). As a solution, one can split of the query string after the ? in a first step, then use [&]language.*?(?=&|\?|$) for the replacement on the query string, and later build the full URL again using path + "?" + replacedQuery.
@AvinashRaj for input /fr/welcome?language=en&a=b gives /fr/welcome&a=b
|
5

You could use regex with replaceAll()

public static void main(String[] args) {
    String s1 = "/fr/welcome?language=en";
    String s2 = "/fr/welcome?param1=222&param2=aloa&language=en";
    String s3 = "/fr/welcome?param1=222&language=en&param2=aa";
    String pattern = "[?&]language=.{2}"; // use pattern = "([?&]language=\\w+)"; for more than 2 letters after language ==.
    System.out.println(s1.replaceAll(pattern, ""));
    System.out.println(s2.replaceAll(pattern, ""));
    System.out.println(s3.replaceAll(pattern, ""));
}

o/p :

/fr/welcome
/fr/welcome?param1=222&param2=aloa
/fr/welcome?param1=222&param2=aa

1 Comment

This messes up the URL if there are more than one query parameters and the first one is "language". In this case, the "?" is removed, so the query parameters are no longer treated as query parameters, but instead as part of the URL path.
1

This regexp should help you:

"language=\\w{2}"

1 Comment

the length is not necessarily 2
1

I would like to remove from each query string the parameter language with its value,...

You can use replaceAll.

String s="/fr/welcome?language=en";
s=s.replaceAll("(\\?|&)language=\\w+", "");
  • (\\?|&) group will match ? or &
  • \\w+ will match one or more word character

1 Comment

This messes up the URL if there are more than one query parameters and the first one is "language". In this case, the "?" is removed, so the query parameters are no longer treated as query parameters, but instead as part of the URL path.
1

This will remove any parameter properly, even if it is placed more than one (for example="/fr/welcome?language=en&param1=222&param2=aloa")

public String removeParamFromUrl(final String url, final String param) {
    if (StringUtils.isNotBlank(url)) {
        return url.replaceAll("&" + param + "=[^&]+", "")
                .replaceAll("\\?" + param + "=[^&]+&", "?")
                .replaceAll("\\?" + param + "=[^&]+", "");
    } else {
        return url;
    }
}

Comments

0

Rather than using a regex, it may be better to use a dedicated URI-manipulation API to remove the query parameter. The Spring UriComponentsBuilder class can be used to remove the given query parameter, retaining the rest. I'm assuming a Spring-specific solution is acceptable, as this question is tagged with .

private static String removeQueryParam(String url) {
    return UriComponentsBuilder.fromUriString(url)
            .replaceQueryParam("language")
            .build()
            .toUriString();
}

From the question as asked, it's unclear why or whether a regex-based solution using String.replaceAll is necessary, or whether instead any Java or Spring-based solution would be acceptable. In other words, this may be an XY problem where the goal is to remove the "language" query parameter while retaining all other query parameters, and there's no particular reason a regex needs to be involved in the solution.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.