【问题标题】:Regular Expression that does not match a given sequence与给定序列不匹配的正则表达式
【发布时间】:2015-08-19 12:08:26
【问题描述】:

我需要一个匹配此文本描述的正则表达式:

"any sequence that is not an F (upper case only) followed by a run of 1 or more digits (0-9)".

这需要作为 Java 的 Scanner.useDelimiter() 的输入。

假设输入行是这样的:

F8 has pretty solid open source cred: in F12 he founded F25, he was the F44 of F1 and the F121, and he helped pave the way for F99.

然后,标记器类 Scanner 应该为我检索 F8、F12、F25、F44、F1、F121 和 F99。

另外,Java 是否允许否定给定的正则表达式?

【问题讨论】:

  • 与逆向拆分相比,匹配表达式本身通常更容易,例如F\d+
  • @tobias_k 确实如此。请参阅下面 Avinash Raj 的答案。

标签: java regex regex-negation


【解决方案1】:

使用 Pattern 和 Matcher 类只获取您想要的字符。

Matcher m = Pattern.compile("\\bF\\d+").matcher(s);
while(m.find()) {
System.out.println(m.group());
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-20
    • 1970-01-01
    • 1970-01-01
    • 2016-11-10
    • 1970-01-01
    相关资源
    最近更新 更多