简体   繁体   English

Java正则表达式:解析具有两个匹配子字符串的字符串

[英]Java regex: Parsing a string with two matching substrings

I have following string 我有以下字符串

task BLABLA@{taskId} "@{BLABLA.title}" 任务BLABLA @ {taskId}“ @ {BLABLA.title}”

and want to extract all placeholders from it. 并希望从中提取所有占位符。

Placeholders are @{taskId} and @{BLABLA.title}. 占位符为@ {taskId}和@ {BLABLA.title}。

I use following code: 我使用以下代码:

final Pattern pattern = Pattern.compile(".*(\\@\\{.*?\\}).*");
final Matcher matcher = pattern.matcher(this.text);

while (matcher.find())
{
    final String placeholder = matcher.group(1);
    this.placeholders.add(placeholder);
}

The problem is that in lines with more than one placeholder (like shown above) it detects only the first placeholder. 问题在于,在具有多个占位符的行中(如上图所示),它仅检测到第一个占位符。

Another example: 另一个例子:

task BLABLA@{taskId} "@{BLABLA.title}" { start @{startDateTime} 任务BLABLA @ {taskId}“ @ {BLABLA.title}” {开始@ {startDateTime}

task BLABLA2 "Text" { allocate RBLABLA2 effort @{BLABLA2.effort} } } 任务BLABLA2“文本” {分配RBLABLA2工作量@ {BLABLA2.effort}}}

In this text, the code above detects 在本文中,上面的代码检测到

  1. @{BLABLA.title} @ {BLABLA.title}
  2. @{startDateTime} @ {startDateTime}
  3. @{BLABLA2.effort} @ {BLABLA2.effort}

If I remove @{BLABLA.title}, then @{taskId} is detected. 如果我删除@ {BLABLA.title},则检测到@ {taskId}。

How should I modify the code so that in the last example, all placeholders (@{taskId}, @{BLABLA.title}, @{startDateTime}, @{BLABLA2.effort}) are detected? 如何修改代码,以便在上一个示例中检测到所有占位符(@ {taskId},@ {BLABLA.title},@ {startDateTime},@ {BLABLA2.effort})?

Remove the greedy wildcard matches ( .* ) at the beginning and end of the expression. 删除表达式开头和结尾的贪婪通配符匹配( .* )。 Your regex would then read: 您的正则表达式将显示为:

"(\\@\\{.*?\\})"

Having removed the wildcards, you can also omit grouping: 删除通配符后,您还可以省略分组:

"\\@\\{.*?\\}"

Remove the leading and ending .* because they eat your whole string. 删除开头和结尾的。*,因为它们会占用您的整个字符串。 In your loop replace m.group(1) by m.group(0) 在循环中,将m.group(1)替换为m.group(0)

//Another way to solve problem
String task = "task BLABLA@{taskId} \"@{BLABLA.title}";
String splitBy = "\\@\\{";
String[] splitted = task.split( splitBy );
Set<String> placeHolders = new HashSet<String>();
for( String split : splitted ) {
  int startOf = split.indexOf("}");
  if( startOf != -1 ) {
     placeHolders.add(split.substring( 0, startOf));
  }
 }
 System.out.println("place holders are " + placeHolders);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM