如何查找括号内的元素

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

How to find element in brackets

问题

我需要文本字段中括号内的注释值。示例:

```java
String text = "I have simple annotation @Test(value123) and ..";

我可以找到带有值的注释本身,但我不明白如何在没有注释的情况下获取它。

Pattern patternString = Pattern.compile("@Test\\(\\s+\\)");

结果:

>@Test(value123)

但我需要

>value123


<details>
<summary>英文:</summary>

I need the value in parentheses for the annotation in the text field. Example:

```String text = &quot;I have simple annotation @Test(value123) and ..```

I can find the annotation itself with a value, but I don&#39;t understand how to get it without an annotation
```Pattern patternString = Pattern.compile(&quot;@Test\\(\\s+\\)&quot;);```

Result: 

&gt;@Test(value123)

but I need 

&gt;value123


</details>


# 答案1
**得分**: 2

添加一个(X)[捕获组][1]:

```lang-java
String text = "I have simple annotation @Test(value123) and ..";
Pattern p = Pattern.compile("@Test\\(([^)]*)\\)");
Matcher m = p.matcher(text);
if (m.find()) {
    System.out.println(m.group(1)); // 打印捕获组 1
}

输出

value123

解释

@Test\(     匹配 '@Test('
(           开始捕获组 1
  [^)]*       匹配零个或多个字符,不包括')'
)           结束捕获组 1
\)          匹配 ')'

或者,使用 (?<=X) 零宽度正回顾后发断言(?=X) 零宽度正先行断言

String text = "I have simple annotation @Test(value123) and ..";
Pattern p = Pattern.compile("(?<=@Test\\()[^)]*(?=\\))");
Matcher m = p.matcher(text);
if (m.find()) {
    System.out.println(m.group()); // 打印匹配的文本
}

输出

value123

解释

(?<=          开始零宽度正回顾后发断言
  @Test\(       匹配 '@Test('
)             结束零宽度正回顾后发断言
[^)]*         匹配零个或多个字符,不包括')'
(?=           开始零宽度正先行断言
  \)            匹配 ')'
)             结束零宽度正先行断言
英文:

Add a (X) capturing group:

String text = &quot;I have simple annotation @Test(value123) and ..&quot;;
Pattern p = Pattern.compile(&quot;@Test\\(([^)]*)\\)&quot;);
Matcher m = p.matcher(text);
if (m.find()) {
	System.out.println(m.group(1)); // Print capture group 1
}

Output

value123

Explanation

@Test\(     Match &#39;@Test(&#39;
(           Start of capturing group 1
  [^)]*       Match zero-or-more characters, except &#39;)&#39;
)           End of capturing group 1
\)          Match &#39;)&#39;

Alternatively, use (?&lt;=X) zero-width positive lookbehind and (?=X) zero-width positive lookahead:

String text = &quot;I have simple annotation @Test(value123) and ..&quot;;
Pattern p = Pattern.compile(&quot;(?&lt;=@Test\\()[^)]*(?=\\))&quot;);
Matcher m = p.matcher(text);
if (m.find()) {
	System.out.println(m.group()); // Print matched text
}

Output

value123

Explanation

(?&lt;=          Start of zero-width positive lookbehind
  @Test\(       Match &#39;@Test(&#39;
)             End of zero-width positive lookbehind
[^)]*         Match zero-or-more characters, except &#39;)&#39;
(?=           Start of zero-width positive lookahead
  \)            Match &#39;)&#39;
)             End of zero-width positive lookahead

答案2

得分: 0

你可以使用一个匹配器,就像这样:

import java.util.regex.Pattern;
import java.util.regex.Matcher;

class Main {
  public static void main(String[] args) {
    String testString = "@Test(value123)";
    // 未转义的括号捕获组
    // 注意我将 \\s+ 更改为 .+,
    // 因为我想匹配括号内的任何内容,
    // 而不是空格(但是,您应根据自己的需求进行调整)
    Pattern pattern = Pattern.compile("@Test\\((.+)\\)");
    Matcher matcher = pattern.matcher(testString);
    if (matcher.matches()) {
      String value = matcher.group(1);
      System.out.println(value); // 输出 value123
    }
  }
}
英文:

You can use a matcher, like this:

import java.util.regex.Pattern;
import java.util.regex.Matcher;

class Main {
  public static void main(String[] args) {
    String testString = &quot;@Test(value123)&quot;;
    // unescaped parentheses catch the group
    // note that I changed \\s+ to .+,
    // since I want to match whatever is inside the parentheses,
    // not whitespaces (however, you should adjust it to your needs)
    Pattern pattern = Pattern.compile(&quot;@Test\\((.+)\\)&quot;);
    Matcher matcher = pattern.matcher(testString);
    if (matcher.matches()) {
      String value = matcher.group(1);
      System.out.println(value); // prints value123
    }
  }
}

huangapple
  • 本文由 发表于 2020年10月22日 18:53:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/64480733.html
匿名

发表评论

匿名网友

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

确定