如何在正则表达式的“或”(|)条件中精确匹配一个单词。

huangapple go评论81阅读模式
英文:

how to match exactly one word among the others in REGEX 'or'(|) condition

问题

以下是您要求的翻译内容:

我知道有类似的问题,但它们似乎都不符合我的要求。我对正则表达式还不熟悉,并且仍在学习中。我感谢任何关于以下问题的帮助。
我有以下输入 JSON 数据。附注:为了更好地理解我的问题,我已经简化了实际数据。
1.

{
  "name": "abc",
  "response": {
    "postback": "UNIQUE-a651-95e4834b63cc",
    "text": "testing"
  },
  "remarks": "get keyword"
}
{
  "name": "abc",
  "response": {
    "postback": "a651-95e4834b63cc",
    "text": "testing"
  },
  "remarks": "get keyword"
}

正则表达式

(UNIQUE)|(\"(response|someothedata|otherdata|somedata)\")

我想要知道这些关键字中哪些是匹配的。如果 'UNIQUE' 字段存在,它应该立即停止匹配,不再继续匹配。
我正在使用以下 Java 代码

pattern = Pattern.compile(""(UNIQUE)|(\"(response|someothedata|otherdata|somedata)\")"");
Matcher matcher = pattern.matcher(message);
if(matcher.find()){
  match = matcher.group();
}

对于两个输入 JSON,matcher.group() 返回 'response'。

我想要实现以下:

对于 1. 调用 group() 返回 'UNIQUE'
对于 2. 调用 group() 返回 'response',即仅在 'UNIQUE' 不存在时返回

注意:一旦我获得匹配的单词(match),根据它需要执行一些操作,所以不能妥协。

请问有谁可以帮助我如何以某种方式给予 'UNIQUE' 关键字优先权?

英文:

I am aware that there are similar questions to this but none of them seem to match my requirement. I am new to regex and still learning. I appreciate Any help with following problem.
I have following input json data. PS: I have simplified the actual data for better understanding of my problem.
1.

{
  "name": "abc",
  "response": {
    "postback": "UNIQUE-a651-95e4834b63cc",
    "text": "testing"
  },
  "remarks": "get keyword"
}
{
  "name": "abc",
  "response": {
    "postback": "a651-95e4834b63cc",
    "text": "testing"
  },
  "remarks": "get keyword"
}

Regex

(UNIQUE)|(\"(response|someothedata|otherdata|somedata)\")

I want to get which of these keywords is matched. here it gets trickier if 'UNIQUE' field is present it should stop right there and not match any further.
I am using following java code

pattern = Pattern.compile("(UNIQUE)|(\"(response|someothedata|otherdata|somedata)\")")  
Matcher matcher = pattern.matcher(message);  
if(matcher.find()){
  match = matcher.group();
}  

for both input json, matcher.group() returns 'response'

I want to achieve following :

for 1. group() to return 'UNIQUE'
for 2. to return 'response' i.e, only if 'UNIQUE' not present

Note: once I get the matched word(match) there are few actions to be done based on that, so that can't be compromised

can anyone please help me how I can somehow prioritize 'UNIQUE' keyword?

答案1

得分: 1

你可以使用

Pattern pattern = Pattern.compile(".*(UNIQUE|\"(?:response|someothedata|otherdata|somedata)\")");
Matcher matcher = pattern.matcher(message);
if(matcher.find()){
  match = matcher.group(1).replaceAll("^\"|\"$", "");
}

查看正则表达式演示。详细信息:

  • .* - 任意零个或多个非换行字符,尽可能多(这将将正则表达式索引移动到字符串/行的末尾)
  • (UNIQUE|\"(?:response|someothedata|otherdata|somedata)\") - 第1组:UNIQUE\",后面跟responsesomeothedataotherdata
    somedata,然后是 \"

你正在匹配模式的单个出现,这意味着对于你来说,匹配是在开头还是在结尾都无关紧要。这就是为什么在一个捕获组中添加 .* 和分组 UNIQUE\"(?:response|someothedata|otherdata|somedata)\" 就会起作用。唯一的“问题”是,response 等单词两端会带有双引号,所以你可以安全地用 .replaceAll("^\"|\"$", "") 将它们删除。

英文:

You can use

Pattern pattern = Pattern.compile(".*(UNIQUE|\"(?:response|someothedata|otherdata|somedata)\")");
Matcher matcher = pattern.matcher(message);  
if(matcher.find()){
  match = matcher.group(1).replaceAll("^\"|\"$", "");
}  

See the regex demo. Details:

  • .* - any zero or more chars other than line break chars, as many as possible (this moves the regex index to the end of string/line)
  • (UNIQUE|\"(?:response|someothedata|otherdata|somedata)\") - Group 1: UNIQUE or ", followed with either response, someothedata, otherdata
    or somedata and then "

You are matching a single occurrence of a pattern, and that means it does not matter for you whether the match is first or last. That is why adding .* and grouping UNIQUE and "(?:response|someothedata|otherdata|somedata)" in one capturing group will work. The only "problem" is that the response, etc. words will come with double quotes on both ends, so you can safely remove them with .replaceAll("^\"|\"$", "").

huangapple
  • 本文由 发表于 2020年10月16日 23:50:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/64392459.html
匿名

发表评论

匿名网友

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

确定