Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
199 views
in Technique[技术] by (71.8m points)

python - Regular expression to match any number except for one containing repeated zeros

I want to match large numbers in my data but sometimes the data includes repeated zeros for emphasis rather than phone numbers. How can I identify numbers at least nine digits long and not include any numbers with repeated zeros (say, at least 5)? For example:

match: call me at 19083910893

don't match: this x1000000000

I tried [0-9]+(?!0+) but this isn't what I need because the negative lookahead doesn't unmatch results found with the [0-9]+. Somehow, (d)(?!0+)d+ works in tests but I don't really understand why.

question from:https://stackoverflow.com/questions/65840043/regular-expression-to-match-any-number-except-for-one-containing-repeated-zeros

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You could use

(?<!d)(?!d*0{5})d{9,}

Explanation

  • (?<!d) Negative lookbehind, assert not a digit directly to the left
  • (?!d*0{5}) Assert that at the right are not 5 zeroes
  • d{9,} Match 9 or more digits

Regex demo


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...