英文:
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 = "I have simple annotation @Test(value123) and ..```
I can find the annotation itself with a value, but I don't understand how to get it without an annotation
```Pattern patternString = Pattern.compile("@Test\\(\\s+\\)");```
Result:
>@Test(value123)
but I need
>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 = "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)); // Print capture group 1
}
Output
value123
Explanation
@Test\( Match '@Test('
( Start of capturing group 1
[^)]* Match zero-or-more characters, except ')'
) End of capturing group 1
\) Match ')'
Alternatively, use (?<=X)
zero-width positive lookbehind and (?=X)
zero-width positive lookahead:
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()); // Print matched text
}
Output
value123
Explanation
(?<= Start of zero-width positive lookbehind
@Test\( Match '@Test('
) End of zero-width positive lookbehind
[^)]* Match zero-or-more characters, except ')'
(?= Start of zero-width positive lookahead
\) Match ')'
) 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 = "@Test(value123)";
// 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("@Test\\((.+)\\)");
Matcher matcher = pattern.matcher(testString);
if (matcher.matches()) {
String value = matcher.group(1);
System.out.println(value); // prints value123
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论