Sinan Aydın
Sinan Aydın

Reputation: 69

Python: How can I find text between certain words in a string?

For example, there is a string or txt

"""
asfas @111 dfsfds @222 dsfsdfsfsd dsfds
dsfsdfs sdfsdgsd @333 dsfsdfs dfsfsdf @444 dfsfsd
dsfssgs sdsdg @555 fsfh
"""

Desired result:

"""
@111
@222
@333
@444
@555
"""

Using the code below, I can only see the first result.

import re
html="asfas @111 dfsfds @222 dsfsdfsfsd dsfds"
result = re.search('@"(.+?) ', html)
x = (result.group(0))
print(x)

How do I improve my code?

Upvotes: 0

Views: 331

Answers (3)

Prayson W. Daniel
Prayson W. Daniel

Reputation: 15568

If you always have @ followed by 3 digits then:

import re

text = '''asfas @111 dfsfds @222 dsfsdfsfsd dsfds
dsfsdfs sdfsdgsd @333 dsfsdfs dfsfsdf @444 dfsfsd
dsfssgs sdsdg @555 fsfh
'''

results = re.findall(r'(@\d{3})', text)

print(results)

So () means keep a pattern that has @ followed by only 3 digits.

Upvotes: 2

Andrej Kesely
Andrej Kesely

Reputation: 195438

You can use re.findall method instead of re.search (re.search searches only for the first location where the regular expression pattern produces a match):

import re

txt = '''asfas @111 dfsfds @222 dsfsdfsfsd dsfds
dsfsdfs sdfsdgsd @333 dsfsdfs dfsfsdf @444 dfsfsd
dsfssgs sdsdg @555 fsfh'''

print(*re.findall(r'@\d+', txt), sep='\n')

Prints:

@111
@222
@333
@444
@555

Upvotes: 4

Arkistarvh Kltzuonstev
Arkistarvh Kltzuonstev

Reputation: 6920

You can do this even without using regex :

html="asfas @111 dfsfds @222 dsfsdfsfsd dsfds"
x = [i for i in html.split() if i.startswith('@')]

Output :

['@111', '@222']

Upvotes: 1

Related Questions