Reputation: 1
I have regex like
m=re.search('z+',"i want to zzz zzzzzzz this is end")
how to find the length of consicutive z ignoring spaces 10(3z+7z)
"example str want to zz z this is end" ->3(2+1)
I am new here, please comment if anything is missing
Upvotes: 0
Views: 78
Reputation: 627334
You can use
import re
matches = re.findall("z+(?:\s+z+)*", "i want to zzz zzzzzz this is end")
z_count = [(s, s.count('z')) for s in matches]
wht_count = [(s, len(s) - s.count('z')) for s in matches]
print( f'Z count: {z_count}\nWhitespace count: {wht_count}' )
# =>
## Z count: [('zzz zzzzzz', 9)]
## Whitespace count: [('zzz zzzzzz', 1)]
See the online Python demo.
Please note that z+(?:\s+z+)*
regex won't capture any leading/trailing whitespace, so the number of spaces will be correct.
The regex matches
z+
- one or more z
letters(?:\s+z+)*
- zero or more repetitions of one or more whitespace and one or more letters z
.See the regex demo.
Upvotes: 0
Reputation: 54767
You will need to use a slightly different regular expression, like z[z ]*
to return a string of z's and spaces, and then use s.count()
to count how many z's there were:
matches = re.findall("z[z ]*", "i want to zzz zzzzzz this is end")
result = [s.count('z') for s in matches]
print( result )
Output:
[9]
Upvotes: 1
Reputation: 15498
You need to use findall
method instead, it will return all matching occurrences. Then you need to map the Len function to return length of the matched substrings of z, and later you can get both it and it's sum:
m=re.findall('z+',"i want to zzz zzzzzzz this is end")
m=list(map(len,m))
print(sum(m),m)
Upvotes: 0