【问题标题】:How to match any digits in front of a certain character?如何匹配某个字符前面的任何数字?
【发布时间】:2013-11-15 14:27:42
【问题描述】:

我想匹配字符 K 前面的所有数字并提取该值。

在示例CARRY18K 中,它将匹配18

可能我只能使用正则表达式来实现这一点,但是如何实现呢? \d+K 这里的表达方式正确吗?

【问题讨论】:

  • 你可以使用:(\d+)K

标签: java regex matcher


【解决方案1】:

I'd like to match all digits that are in front of the character K and extract that value.

你应该使用:

\d+(?=K)

(?=K) 是肯定的前瞻,确保数字后跟K

【讨论】:

  • 前瞻比\d+K好吗?如果有,为什么?
  • @Cruncher:好问题。答案是 OP 想要提取数字,并且前瞻节省了对数字进行分组并从中捕获 $1 的额外步骤。
【解决方案2】:

我会使用这样的东西:

\d+K

如果你想捕获数字:

(\d+)K

【讨论】:

    猜你喜欢
    • 2020-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多