, asked
Views : 4

How to write Python regular expression to get zero or more occurrences within the pattern?

How do I write a Python regular expression to get zero or more occurrences of  '\w' or word(an alphabet, digit or underscore) in the string 'chihua huahua'?

 

1 Answer
Rajendra Dharmkar

*      An asterisk meta-character  in a regular expression indicates 0 or more occurrences of the pattern to its left

The following code matches and prints the zero or more occurrences of the pattern '\w' in the string 'chihua huahua'

import re
s = 'chihua huahua'
result = re.findall(r'\w*', s)
print result

This gives the output

['chihua', '', 'huahua', '']


Advertisements
Related Questions