用Java的Pattern.compile()获取花括号之间的节点值。

huangapple 未分类评论45阅读模式
英文:

Get node value between curly braces with Java Pattern.compile()

问题

以下是翻译好的内容:

我想从以下字符串中提取id的值

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

我已经制定了以下模式:

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

该模式成功评估了代码:https://regex101.com/r/1kJ5Uo/2 但是我不知道如何在模式中去掉剩余的\
评估结果总是为false,因为模式p被视为:/^.*orderCode=20200409174150852, request=\{([^}]*)

有人可以告诉我如何在执行m.matches()时摆脱模式中的额外\,或者提供另一个提取id值或花括号内内容的模式吗?

英文:

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

huangapple
  • 本文由 发表于 2020年4月9日 23:20:37
  • 转载请务必保留本文链接:https://java.coder-hub.com/61124532.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定