【发布时间】:2022-01-05 13:19:06
【问题描述】:
我正在努力使用一个简单的正则表达式,我似乎无法正确处理。
我有一些这样的文字:
This comment is great **[@madeUpUser1](/madeUpUser1)** You said something similar did you mate? **[@madeUpUser2](/madeUpUser2)**
我想最终得到一个数组列表,其中包含括号之间的用户名,即:
0.madeUpUser1
1.madeUpUser2
这是我目前的代码:
List<String> matches = Pattern.compile("\\((.+?)\\)")
.matcher("This comment is great **[@madeUpUser1](/madeUpUser1)** You said something similar did you mate? **[@madeUpUser2](/madeUpUser2)**")
.results()
.map(MatchResult::group)
.collect(Collectors.toList());
但是我得到的是这样的:
0."(/madeUpUser1)"
1."(/madeUpUser2)"
再次,我想要的地方:
0.madeUpUser1
1.madeUpUser2
即没有括号,也没有正斜杠
谁能解释一下我的正则表达式有什么问题?
【问题讨论】:
-
你可以试试
(?<=\(\/)[^)]+(?=\))。 -
我建议调整问题的标题。到目前为止,接受的答案没有“文本提取到数组列表”的解决方案,只有一个可以用来做到这一点的正则表达式。也许它应该听起来像“在括号之间提取字符串,不包括第一个下划线”,或者类似的东西。