1

I have the following string:

String p = "{requestId=146bb94xxxxxR, value=false, tier=S3,ReceivedTime=0}";

I want to extract the request Id value, so want my output to be

146bb94xxxxxR

Here is what I tried

Pattern MY_PATTERN = Pattern.compile("\\requestId=(.*?)\\,");
Matcher m = MY_PATTERN.matcher(p);
while (m.find()) {
    String s = m.group(1); // s now contains "BAR"
}

However i get no output, there is a problem with the regex, but I am not sure how to correct it.

VLAZ
  • 26,331
  • 9
  • 49
  • 67
user3809938
  • 1,114
  • 3
  • 16
  • 35
  • What's with the '\' before and after...? Try `\\brequestId=([^,]*),`. – SamWhan Jul 01 '16 at 10:44
  • Do you really need regex? Using `indexOf()` you could find the indexes of the `=` and `,`. With those indexes you can easily use `substring()` to get the desired output. – Rudey Jul 01 '16 at 10:45
  • following example from http://stackoverflow.com/questions/600733/using-java-to-find-substring-of-a-bigger-string-using-regular-expression, i merely just substituted the values for the square bracket – user3809938 Jul 01 '16 at 10:45
  • 3
    It's look like, JSON string, you can parse string as JSON and then you can get that value easily. – Kishore Reddy Jul 01 '16 at 10:46
  • you can also use simple one as : requestId=(\w+) – Shekhar Khairnar Jul 01 '16 at 10:59

5 Answers5

3

Your regex escapes the r in request and the , which makes no sence. Try

\\brequestId=([^,]*),

which will match a word boundary - \b, then requestId= and then capturing the ID (everything up to the ,)

SamWhan
  • 8,296
  • 1
  • 18
  • 45
3
Pattern.compile("requestId=(.*?),");

You should delete redundant characters escape (\\, and \\r (it means the carriage-return character)) and it will work fine.

Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142
1

You can also try this:

(?<=requestId=)(\\w*)
Maria Ivanova
  • 1,146
  • 10
  • 19
0

You don't need to surround your regex with \ (written as string "\\") like in example mentioned by you earlier in comment: Using Java to find substring of a bigger string using Regular Expression. There \ was used to escape few of regex special characters which ware [ and ].

So your regex Pattern.compile("\\requestId=(.*?)\\,") \\r part will represent \r which is carriage return (one of line separators). So remove that \\ before r. Also you don't need to escape , with \ since it is not regex special character (at least not in this context). So try with:

Pattern MY_PATTERN = Pattern.compile("requestId=(.*?),");
Matcher m = MY_PATTERN.matcher(" {requestId=146bb94xxxxxR, value=false, tier=S3,ReceivedTime=0}");
while (m.find()) {
    String s = m.group(1);
    System.out.println(s);
}
Community
  • 1
  • 1
Dipak Prajapati
  • 486
  • 5
  • 13
  • Can you explain what you mean by "if you put \\ then it search for string starting with \"? Are you talking about \\ in string (like in OP example) or \\ in regex (which will need to be written as "\\\\" in String) - because only second case would be true here. – Pshemo Jul 01 '16 at 10:58
  • yes i am talking about //(Double slash) in string so /(single slash) is an escape character thats why i have written //(Double slash). and you have to match a pattern which starts with **requestId=** not with (/)slash. – Dipak Prajapati Jul 01 '16 at 11:34
  • Problem with this explanation is that slash ``\`` is also special not only in String literal, but also in regex syntax. So writing regex as string `"\\r"` will represent `\r` which in Java's regex engine is interpreted as one of line separators (carriage return), not ``\`` character followed by `r`. – Pshemo Jul 01 '16 at 12:37
  • I edited your answer to add more explanation of OP problem (hope you don't mind). – Pshemo Jul 01 '16 at 12:50
0
public static void main(String[] args) {
        String text = "{requestId=146bb94xxxxxR, value=false, tier=S3,ReceivedTime=0}";
        Pattern p = Pattern.compile("requestId=(.*?),");
        Matcher m = p.matcher(text);
        m.find();
        System.out.println(m.group(1));
}

Output: 146bb94xxxxxR

Hrabosch
  • 1,541
  • 8
  • 12