Home > OS >  Regex pattern to capture numbers only, on a single line, but exclude date pattern
Regex pattern to capture numbers only, on a single line, but exclude date pattern

Time:10-05

Here is my current regex:

\b\d(?:[^\n\d]*\d){14}(?!\d)

Explanation:

  • \b: Matches a word boundary so that first match digit is matched in a separate word
  • [^\n\d]*: Matches 0 or more of any char that is not a digit and not a line break
  • (?:[^\n\d]*\d){14}: Matches 14 digits optionally interspersed by 0 or more non-digits, non-line breaks

The problem with this pattern is that it matches various date pattern that should be excluded as shown in this enter image description here

I need to modify the current regex so that it will not match if it includes a date pattern. Here is the enter image description here

Now, if I could just figure out how to exclude the second pattern date matches from the first pattern of 15-digit matches. That is what I am trying to accomplish.

Any ideas?

CodePudding user response:

You may try this regex:

\b\d(?!(?:\d{0,3}([-\/\\.])\d{1,2}\1\d{1,4})\b(?!\S))(?:[^\n\d]*\d){14}\b

RegEx Demo

Here (?!(?:\d{0,3}([-\/\\\.])\d{1,2}\1\d{1,4})\b(?!\S)) is a negative lookahead pattern to avoid including dates in your matches.

  • Related