Home > Blockchain >  Wny it does not give all positive numbers in the string? Regex in Python
Wny it does not give all positive numbers in the string? Regex in Python

Time:12-05

I don't understand why it only gives 125, the first number only, why it does not give all positive numbers in that string? My goal is to extract all positive numbers.

import re

pattern = re.compile(r"^[ ]?\d ")

text = "125 -898 8969 4788 -2 158 -947 599"

matches = pattern.finditer(text)

for match in matches:
    print(match)

CodePudding user response:

Try using the regular expression

-\d |(\d )

Disregard the matches. The strings representing non-negative integers are saved in capture group 1.

Demo

The idea is to match what you don't want (negative numbers), and if there is no match of that, match and save to a capture group what you do want to match.

Any plus signs in the string can be disregarded.

For a fuller description of this technique see The Greatest Regex Trick Ever. (Search for "Tarzan"|(Tarzan) to get to the punch line.)

CodePudding user response:

The following pattern will only match non negative numbers:

pattern = re.compile("(?:^|[^\-\d])(\d )")

pattern.findall(text)

OUTPUT

['125', '8969', '4788', '158', '599']

CodePudding user response:

Your pattern ^[ ]?\d is anchored at the start of the string, and will give only that match at the beginning.

Another option is to assert a whitspace boundary to the left, and match the optional followed by 1 or more digits.

(?<!\S)\ ?\d \b
  • (?<!\S) Assert a whitespace boundary to the left
  • \ ? Match an optional
  • \d \b Match 1 or more digits followed by a word bounadry

Regex demo

CodePudding user response:

For the sake of completeness another idea by use of \b and a lookbehind.

\b(?<!-)\d 

See this demo at regex101

  • Related