3

Using Regex in Python (library re (only)), I want to create a function that gives me the position of all leading 0s in a string.

For example, if the string was: My house has 01 garden and 003 rooms. I would want me the function to return 13, 27 and 28.

I tried for example:

import re
string = "My house has 01 garden and 003 rooms."
pattern = "(0+)[1-9]\d*"

print(re.findall(pattern,string))

Obviously, the output gives me the matches but no position...

S.B
  • 13,077
  • 10
  • 22
  • 49
Katharina Böhm
  • 125
  • 1
  • 8
  • Does this answer your question? [Find the indexes of all regex matches?](https://stackoverflow.com/questions/3519565/find-the-indexes-of-all-regex-matches) – The Myth Nov 01 '22 at 12:41

1 Answers1

5

You can do the following:

import re

text = "My house has 01 garden and 003 rooms."
pattern = re.compile(r"\b0+")


def leading_zeros_index(s: str) -> list:
    return [i for m in pattern.finditer(s) for i in range(m.start(), m.end())]


print(leading_zeros_index(text))

output:

[13, 27, 28]

Basically you use .finditer() in order to get the match object, then you create a range() object from match object's .start() and .end().

I used \b0+ as the pattern. There is no need to check the other characters come after zeros. \b is word boundary, here means, zeros should be at the start of the words.

S.B
  • 13,077
  • 10
  • 22
  • 49
  • This looks great, thank you! I am just beginning to understand regex, so i need to ask, the r in (r"\b0+") is needed why? – Katharina Böhm Nov 01 '22 at 13:00
  • @KatharinaBöhm That `r""` creates a "raw-string". You should almost always use it for regex patterns because it prevents Python from escaping some escape-sequences. Try to run `print("hi\nbye")` and `print(r"hi\nbye")` to see the difference. – S.B Nov 01 '22 at 13:08