0

For example, if a value matches "an email pattern", then remove the key also associated to it from the url.

If url is

https://stackoverflow.com/?key1=test&key2=test@gmail.com&key3=something&key4=another@email.com,

then remove key2=test@gmail.com and key4=another@email.com, so that the new url will be

https://stackoverflow.com/?key1=test&key3=something

Here, key names are not fixed and they can be anything. and also the position of the keys is not fixed.

So, want a regex to get the entire string which does not contain those key value pairs. I tried to generate the regex to match the unwanted key value pairs, but could get the rest of the string which does not match the regex.

I did it using a java program. But looking at a regex so that I can apply in the xml and avoid a java program

This is mainly to use in urlrewritefilter (tuckey) and want to remove certain query strings matching a regex.

Venu
  • 1,513
  • 3
  • 19
  • 37
  • 1
    You'd better try a url parser. For regex you could do `[?&]key[24]=[^&]+` – revo Mar 29 '18 at 13:20
  • I gave an example of the key as key2 and key4. I mentioned key names are not fixed and unknown. – Venu Mar 29 '18 at 13:22
  • Possible duplicate of [Parse a URI String into Name-Value Collection](https://stackoverflow.com/questions/13592236/parse-a-uri-string-into-name-value-collection) – azro Mar 29 '18 at 13:22
  • See the link given as duplicate, it'll help you – azro Mar 29 '18 at 13:22
  • Is there any max number of keys that come in URL. Also, I think its better to implement it take all key value pairs, put it in map, then compare all the values with email regex and concatenate only those for which regex fails – Aman Chhabra Mar 29 '18 at 13:22
  • Regex basics `[?&](?:a-key|another-key)=[^&]+` – revo Mar 29 '18 at 13:23

1 Answers1

0

Here is a simple solution in java (I saw your question is tagged as java). This is basically pattern that matches ? or & followed by a word then a = and then an email. You can substitute that part [.\w]+@[\w]+\.\w+ with a better email regex. Finding email with regex can be tricky with stranger emails but this would be the basic idea.

public class HelloWorld{

 public static void main(String []args){
    String url="https://stackoverflow.com/?key1=test&key2=test@gmail.com&key3=something&key4=another@email.com";
    System.out.println(url.replaceAll("[?&]\\w+=[.\\w]+@[\\w]+\\.\\w+",""));
 }
}
Veselin Davidov
  • 7,031
  • 1
  • 15
  • 23