regex for excluding certain phrase from txt.file

Viewed 61

I need to retrieve numbers from txt file which looks +/- like this:

[  Index 1  ]
1628 5704
32801 61605
71508 90612
1026061

I need to ignore Indexe's number.

[0-9]+ retrieve all numbers, index as well.

I tried something like this called negative lookahead (?![(Index 1)])([0-9]+). It ignores 1 indeed, but all of them... for instance 1628 becomes 628. Appreciate for help, I've always been weak in regex syntax :/

2 Answers

Use

\b(?<!Index )\d+

See proof.

Explanation

--------------------------------------------------------------------------------
  \b                       the boundary between a word char (\w) and
                           something that is not a word char
--------------------------------------------------------------------------------
  (?<!                     look behind to see if there is not:
--------------------------------------------------------------------------------
    Index                    'Index '
--------------------------------------------------------------------------------
  )                        end of look-behind
--------------------------------------------------------------------------------
  \d+                      digits (0-9) (1 or more times (matching
                           the most amount possible))

This pattern matches only numbers. It's looking for a series or multiple digits at the start of the string or series or multiple digits at the end of a string.

^\d+|\d+$

https://regex101.com/r/ZNTxQ7/1

Another way is to look for a series of two or more digits in the string.

\d{2,}

https://regex101.com/r/Di75KT/1

Related