-1

I've got a request parameter string that will always come to my server like so:

str1=wordA&str2=wordB&desc3=&int=0etc.

The catch here is the parameter with name desc3, which may or may not have data accompanied with it, as I've shown here, with no data. What I'm trying to do is use regex to extract strings that match the right hand side of str1=word, while also being able to specify what is on the left hand side. For instance, I may want only str1 params, or I may want only descparams. Each one of these will be a seperate regex. For instance, the str1 regex will be separate from the desc3 regex. I am very new to working with regex, and I can't get the results no matter what I seem to do.

REAL O G
  • 693
  • 7
  • 23

3 Answers3

1

Let's say you have this:

String your = "str1=wordA&str2=wordB&desc3=&int=0"

Now you do:

String[] split = your.split("&");

Now split looks like this:

["str1=wordA", "str2=wordB", "desc3=", "int=0"

Now if you want to get let's say second parameter you can:

String[] secondParameter = split[1].split("=");

Now secondParameter[0] is "str2" and secondParameter[1] is "wordB". If you wan to get desc3 you do:

String[] descParam = split[2].split("=");
String name = descParam[0];
String value = "";
if(descParam.length > 1)
  value = descParam[1];
else
  value = "Parameter desc3 has no value specified";

I hope this helps. The code is kinda self-explanatory, split just takes your string and 'divides' it by regex that you specify as a parameter and return an array.

Shadov
  • 5,421
  • 2
  • 19
  • 38
  • I actually have already done this, down to your last two lines. The issue there though is that this needs to scale, meaning that if changes get made to the parameters that come in, I won't have to manually tweak which indices I'm accessing. That's why I'm trying to match on the left hand side only. For instance, right now there are only two parameters, with the name `str1` & `str2`, but if this list increases to 15, I would have to manually access each one and know in advance which ones belong to `str` and not `desc` or `int`. – REAL O G Sep 19 '16 at 18:54
  • You can just iterate through it and check, or put this in a map with parameter name as the key, there is tons of options. Oh, look at that answer below, it shows how you can put that in a map right away. – Shadov Sep 19 '16 at 18:56
1

Sometimes a regex isn't the correct way to do it. I would try a string split approach:

    String testString = "str1=wordA&str2=wordB&desc3=&int=0";
    Map<String, String> mapOfParams = Arrays.stream(testString.split("&"))
            .collect(Collectors.toMap(s -> s.split("=")[0],
                                      s -> s.split("=").length > 1 ? s.split("=")[1] : ""));

This will give you a Map with empty values as empty strings.

STeve Shary
  • 1,365
  • 1
  • 8
  • 9
1

This should work:

String desc3 = input.replaceAll(".*desc3=([^&]*).*", "$1");

Where desc3 is whatever parameter you want the value for.

This matches the whole input, but captures the value and returns it in the replacement via a backreference.


To just get 'em all, first split on "&", then split each of those on "=", then collect them into a map:

Map<String, String> pairs = Arrays.stream(input.split("&"))
    .map(s -> s.split("=", -1)) // Note the -1!
    .collect(Collectors.toMap(a -> a[0], a -> a[1]));

Try it online!

Note how you must use a negative 2nd parameter to the second split, which force the split to not discard trailing blank elements. Without it, splitting "desc3=" on "=" would result in just ["desc3"], but with -1 you get "desc3", ""]

Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • This works, although it would only work if I manually input each parameter that I needed. I was looking for a convenient way to get say all `str` parameter values or all `desc` parameter values. – REAL O G Sep 19 '16 at 19:25
  • @gd that's what you said you wanted: *Each one of these will be a seperate regex* – Bohemian Sep 19 '16 at 19:36
  • Sorry, I should have clarified. The regex would be separated by category. i.e. one regex for `str` parameter names, one regex for `int` parameter names, one regex for `desc` parameter names, etc. – REAL O G Sep 19 '16 at 19:42
  • @gd see new golden hammer solution – Bohemian Sep 19 '16 at 22:09