【发布时间】:2009-04-13 17:40:58
【问题描述】:
我正在尝试找出我遇到的 Java 正则表达式问题。这是1.6,但我认为这并不重要。总之……
我将有一些输入数据,如下所示...
"Blah yadda yidda 44-Barack Obama, this that the other"
或
"Something here, there 22-Hyphenated-example. Hi there folks"
基本上,我想提取从数字到尾随标点的所有内容。在我要提取的两个示例输入中...
"Barack Obama"
和
"Hyphenated-example"
我无法完全理解我需要使用的模式。我能得到的最接近的是这个......
"[0-9]{1,2}-([A-Z -]*\\b*)"
然而,这给了我......
"44-Barack Obama"
我的代码是...
Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher("Blah yadda yidda 44-Barack Obama, this that the other");
if (matcher.find())
// This gives me "44-Barack Obama" but I want "Barack Obama".
System.out.println(matcher.group());
有趣的是,我正在使用 QuickREx Eclipse 插件来测试这个模式,它会返回正确的值。但是,运行上面的代码并没有。
有什么想法吗?
【问题讨论】: