Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)

I need to retrieve the numbers from a text 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 the 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 the regex syntax.

1 Answer

0 votes
by (36.8k points)

Use the below code:

\b(?<!Index )\d+

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))

 Want to be a master in Data Science? Enroll in this Data Science Courses

Browse Categories

...