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.

5
  • What's with the '\' before and after...? Try \\brequestId=([^,]*),. Commented Jul 1, 2016 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. Commented Jul 1, 2016 at 10:45
  • following example from stackoverflow.com/questions/600733/…, i merely just substituted the values for the square bracket Commented Jul 1, 2016 at 10:45
  • 3
    It's look like, JSON string, you can parse string as JSON and then you can get that value easily. Commented Jul 1, 2016 at 10:46
  • you can also use simple one as : requestId=(\w+) Commented Jul 1, 2016 at 10:59

5 Answers 5

3
Pattern.compile("requestId=(.*?),");

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

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

3 Comments

Just an observation, this does it in 41 steps, mine in 16 ;)
@ClasG, and mine in 28. So I guess yours is the most efficient. :)
@ClasG, I just corrected OP's solution, yours is good one, I've upvoted
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 ,)

Comments

1

You can also try this:

(?<=requestId=)(\\w*)

3 Comments

Why the [] around \w? (?<=requestId=)(\\w*) will do it (including the java escape).
OK. Edited. Just a habit, I guess.
I edited it now and escaped the slash. You should have no issues now.
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

Comments

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);
}

4 Comments

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.
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.
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.
I edited your answer to add more explanation of OP problem (hope you don't mind).

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.