Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python regex, how to match a string for n times of occurrences

Tags:

python

regex

I want to match the line that contains word 100.00% for 3 occurrences.

e.g

some string 100.00% foo 100.00% 100.00%
some string  99.91%  foo 100.00% 99.91%
some string 100.00%100.00%100.00%

So line 1 and 3 should all be matched.

However, my following regex seems only to match the case that has 100.00% repeated for 3 times. (Only match line 3)

re.search(r"([1][0][0]\.[0][0][%]){3}",string)

I wonder how should I do to match line 1 as well?

Thanks everybody!

like image 996
peterj Avatar asked Oct 21 '25 19:10

peterj


2 Answers

Halemur Ali's answer is much cleaner than this one which I leave only for completeness.

The notation {3} is merely a shortcut, you can always replace it by expanding the repeated sequence. To match line where the sequence 100.00% appears exactly 3 times, you can use:

"^(?:(?!100\.00%).)*(?:100\.00%(?:(?!100\.00%).)*){3}$"

This uses negative-lookaheads.

It can be read this way:

  • ^ start of the line
  • (?:(?!100\.00%).)* any character (zero or more), until a 100.00% sequence
  • (?:100\.00% the 100.00% sequence
  • (?!100\.00%).)* followed by any character (zero or more) until a 100.00% sequence
  • {3} repeated 3 times
  • $ end of line

Consider taking a look at python's documentation of the re module.

Note: brackets are not necessary to match a single character.

like image 53
silel Avatar answered Oct 23 '25 07:10

silel


an alternative that requires a simpler regular expression is to find all substrings that match 100.00% and test if the count == 3.

example

import re

p = re.compile(r'100\.00%')
texts = ['some string 100.00% foo 100.00% 100.00%',
         'some string  99.91%  foo 100.00% 99.91%',
         'some string 100.00%100.00%100.00%']

matches = [i for i, t in enumerate(texts)
           if len(re.findall(p, t)) == 3]
# matches = [0, 2]
like image 38
Haleemur Ali Avatar answered Oct 23 '25 07:10

Haleemur Ali



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!