0

I need to enumerate through a string finding all the [ ] and getting the text in between and replacing it all with something different.

For example:

http:\www.domain.com\id=[USER_ID]&record=[CALL_KEY]&someother=[OTHER_STUFF]

As it stands now I have to hard code each individual key and replace it with the value.

private String injectValues(String url) {

    String injected = url.replace("[CALL_KEY]", "123");
    injected = injected.replace("[USER_ID]", "some user ide");
    injected = injected.replace("[OTHER_STUFF]", "somethingElse");

    return injected;

}

I have a bunch of different values that can be plugged in, and instead of hard coding [CALL KEY] etc, I would like to dynamically read what is in there and inject the value.

I have used a RegEx enumerateMatchesInString in Objective-C, is there an Android/Java equivalent?

I have tinkered with Pattern but I don't' know enough about it to make it work for what I want.

private void injectValues(String url) {

        Pattern pattern = Pattern.compile("[(.+?)]");
        Matcher matcher = pattern.matcher(url);

....///EEEEEHH  how do I get the matches?
         String key = ;//THE VALUE INSIDE THE SQUARE BRACKETS
         String aValue = someMethodToGetTheValueForKey(key);

}

 private String someMethodToGetTheValueForKey(String key){
        return "Something";
    }
user-44651
  • 3,924
  • 6
  • 41
  • 87
  • there are already a multitude of questions about parsing querystring – Patrick Parker Feb 15 '17 at 18:43
  • Oh great, did you care to give some URLs to questions that relate? – user-44651 Feb 15 '17 at 18:44
  • Possible duplicate of [Parse a URI String into Name-Value Collection](http://stackoverflow.com/questions/13592236/parse-a-uri-string-into-name-value-collection) – Patrick Parker Feb 15 '17 at 18:45
  • That doesn't really fit, since that solution is breaking at `&`. My string may have `&` but no `[TEXT]`. I am looking for a way to take a string and find `[TEXT]` and replace it with something else. Any string. It doesn't have to be a URL. I was using that as an example. – user-44651 Feb 15 '17 at 18:48
  • 1
    Can't you just maintain a dictionary of key value pairs and do the replacements in a loop? String injected = url; for(String key : properties.keySet()) { injected = injected.replace("[" + key + "]", properties.get(key); } I don't see the need for a generic regex since you can only replace the values for known keys. – Ehz Feb 15 '17 at 19:11

2 Answers2

0

This is what you are missing:

// how do I get the matches?
while(matcher.find()) {
     String key = matcher.group(1); //THE VALUE INSIDE THE SQUARE BRACKETS

Also, you may want to take a look at the example code in the documentation of Matcher.appendReplacement and use it as a starting point.

Patrick Parker
  • 4,863
  • 4
  • 19
  • 51
0

I suggest a very simple aproach.

If you could use Apache commons, yo could do:

String s = "id=[USER_ID]&record=[CALL_KEY]&someother=[OTHER_STUFF]";
String[] arrayKeys = StringUtils.substringsBetween(s, "[", "]")

The arrayKeys will contains the keys between " [ ] ". Then you should replace "["+arrayKeys[0]+"]" for your value for example.

melli-182
  • 1,216
  • 3
  • 16
  • 29