java - 我如何在java中使用正则表达式提取最后一个最短的字符串

标签 java regex shortest

我如何提取下面的粗体范围字符串

string :

  1. hello world blah -d blah vlaah -n blah vlahh
  2. hello world blah -n blah vlahh -d blah vlaah
  3. hello world blah -d blaaah

我试过了。 -[dn] .*$ 但它找到了最长的匹配字符串,如下所示

  1. hello world blah -d blah vlaah -n blah vlahh

我想提取最短的匹配字符串。提前致谢

最佳答案

您可以使用 negative lookahead避免在匹配中匹配另一个 -d/-n:

-[dn] (?!.*?-[dn]).*$

RegEx Demo

关于java - 我如何在java中使用正则表达式提取最后一个最短的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31545405/

相关文章:

java - 我无法打印转换后的字符串的值

javascript - 为 CodeMirror 创建新模式

algorithm - 穿过唯一 x 点的最短路径

algorithm - 最短路径算法设计

java - 如何通过java shell调用cygwin?

java - Apache Felix OSGi 错误 : missing requirement osgi. 扩展程序

java - 除非 GPS 开启,否则融合位置提供程序无法获取位置

javascript - 正则表达式:替换单词前面

javascript - 正则表达式奇怪地不起作用

c++ - BFS打印最短路径