简体   繁体   English

如何使正则表达式提前匹配一位和两位数字?

[英]How to make Regex lookahead to match one and two digit numbers?

Let's say for example that I have a string reading "1this12string" .例如,假设我有一个字符串读取"1this12string" I would like to use String#split with regex using lookahead that will give me ["1this", "12string"] .我想使用String#split和正则表达式,使用前瞻,这会给我["1this", "12string"]

My current statement is (?=\d) , which works very well for single digit numbers.我目前的陈述是(?=\d) ,它非常适用于个位数。 I am having trouble modifying this statement to include both 1 and 2 digit numbers.我无法修改此语句以同时包含 1 位和 2 位数字。

Add a look behind so you don't split within numbers:在后面添加一个外观,这样您就不会数字中分裂:

(?<!\d)(?=\d)

See live demo .现场演示

If you really want to use Regex Lookahead, try this:如果你真的想使用 Regex Lookahead,试试这个:

(\d{1,2}[^\d]*)(?=\d|\b)

Regex Demo正则表达式演示

Note that this assume every string split must have 1 or 2 digits at the front.请注意,这假设每个字符串拆分的前面必须有 1 或 2 位数字。 In case this is not the case, please let us know so that we can further enhance it.如果不是这种情况,请告诉我们,以便我们进一步增强它。

Regex Logics正则表达式逻辑

  • \d{1,2} to match 1 or 2 digits at the front \d{1,2}匹配前面的 1 或 2 位数字
  • [^\d]* to match non-digit characters following the first 1 or 2 digit(s) [^\d]*匹配前 1 或 2 位数字后的非数字字符
  • Enclose the the above 2 segments in parenthesis () so as to make it a capturing group for extraction of matched text.将上述2段用括号()括起来,使其成为提取匹配文本的捕获组。
  • (?=\d to fulfill your requirement to use Regex Lookahead (?=\d以满足您使用 Regex Lookahead 的要求
  • |\b to allow the matching text to be at the end of a text (just before a word boundary) |\b允许匹配的文本位于文本的末尾(就在单词边界之前)

I think you can also achieve your task with a simpler regex, without using the relatively more sophisticated feature like Regex Lookahead.我认为您也可以使用更简单的正则表达式来完成您的任务,而无需使用像 Regex Lookahead 这样相对更复杂的功能。 For example:例如:

\d{1,2}[^\d]*

You can see in the Regex Demo that this works equally well for your sample input.您可以在Regex Demo中看到这同样适用于您的示例输入。 Anyway, in case your requirement is anything more than this, please let us know to fine-tune it.无论如何,如果您的要求不止于此,请告诉我们进行微调。

Use利用

String[] splits = string.split("(?<=\\D)(?=\\d)");

See regex proof正则表达式证明

Explanation解释

--------------------------------------------------------------------------------
  (?<=                     look behind to see if there is:
--------------------------------------------------------------------------------
    \D                       non-digits (all but 0-9)
--------------------------------------------------------------------------------
  )                        end of look-behind
--------------------------------------------------------------------------------
  (?=                      look ahead to see if there is:
--------------------------------------------------------------------------------
    \d                       digits (0-9)
--------------------------------------------------------------------------------
  )                        end of look-ahead

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM