java - 与给定序列不匹配的正则表达式

标签 java regex regex-negation

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

"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 是否允许否定给定的正则表达式?

最佳答案

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

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

关于java - 与给定序列不匹配的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32095103/

相关文章:

java - HikariCP Postgresql 驱动程序声称不接受 JDBC URL

regex - 首字母缩略词的正则表达式

javascript - 分割字符串正则表达式

regex - 用 <pre> 标签内的方括号 (<>) 替换除 <pre> 标签内的换行符

c++ - Mingw32-w64 GCC 4.9.2 正则表达式错误,还是我不懂 C++?

java - 如何检测未引用或双引号的空格

java - Gradle多项目测试未调用

java - 正则表达式 - 禁用同一行的特定子字符串

Java类问题(一对多-多对一)?

Python - 使用正则表达式将数字和字母拆分为子字符串