0

I want to extract the value of id from the following string

.... code=1234random22211, request={ id='randomValue like: aa1a-bb2b-cc3c-ddd4d', event='new' version='1'} ...

I have the made the following pattern:

    Pattern p = Pattern.compile("/^.*code=" + currentCode + ", request=\\{([^}]*)");
    Matcher m = eventIdPatterns.matcher(logs);
    boolean idExists = m.matches();

The pattern evaluates the code successfully: https://regex101.com/r/1kJ5Uo/2 but I don't know how to get rid of the remaining \ in the pattern. The evaluation is always false because pattern p is seens as: /^.*orderCode=20200409174150852, request=\{([^}]*).

Can somebody indicate me how to get rid of that extra \ from pattern when I perform m.matches() or another pattern to extract the value of id or the content between curly braces

vasilev21
  • 113
  • 2
  • 4
  • 15
  • The `/^` is wrong, replace with `^`. There cannot be any chars before the start of string. Also, you can't use `matches()` if you do not expect a whole string match, use `find` then (and remove `^.*` then). Or, add `.*` at the end. – Wiktor Stribiżew Apr 09 '20 at 15:22
  • I replaced it but the code still fails because request=\{([^}]*) is the same with extra \ – vasilev21 Apr 09 '20 at 15:25
  • There is no problem with the backslash in your pattern, it just does not match the whole string. Use `find` after removing `/^.*` – Wiktor Stribiżew Apr 09 '20 at 15:26
  • 1
    thanks Wiktor, m.find() worked for me. – vasilev21 Apr 09 '20 at 15:39

0 Answers0