Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (47.6k points)

I have a string variable containing

string = "123hello456world789"

string contains no spaces. I want to write a regex such that prints only words containing(a-z) I tried a simple regex

pat = "([a-z]+){1,}"

match = re.search(r""+pat,word,re.DEBUG)

a match object contains only the word Hello and the word World is not matched.

When is used re.findall() I could get both Hello and World.

My question is why we can't do this with re.search()?

How do this with re.search()?

1 Answer

0 votes
by (106k points)

You can use the below-mentioned code to understand the re.search in Python:-

>>> import re 

>>> regex = re.compile(r'([a-z]+)', re.I) 

>>> regex.search("123hello456world789").groups() 

('hello',) 

>>> regex.findall("123hello456world789") 

['hello', 'world']

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
4 answers

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...