Python Forum
question: finding multiple strings within string
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
question: finding multiple strings within string
#1
I am trying to figure out how to do the following. I have a string "djfk83dhfdog83748djfk83dhfcat83748djfk83dhfmonkey83748djfk83dhfhuman83748" and I want to be able to pull out the words: dog, cat, monkey and human into three separate strings. Each of these words are surrounded on either side by the characters "djfk83dhf" and "83748". How would I do this with Python 3?
Reply
#2
You could use a regex.
>>> s = "djfk83dhfdog83748djfk83dhfcat83748djfk83dhfmonkey83748djfk83dhfhuman83748"
>>> import re
>>> re.findall(r"djfk83dhf(.+?)83748", s)
['dog', 'cat', 'monkey', 'human']
Reply
#3
I have another question. Lets say you have a slightly different setup. For example, lets say the first set of characters is instead <a href="/quote/ and the last character is ? such that the string would now read <a href="/quote/dog?<a href="/quote/cat?<a href="/quote/monkey?<a href="/quote/human?. How would you get the words: dog, cat, monkey, and human out of this? I tried with the code provided and it doesn't seem to work as I would have expected. The code I tested is:

import re
g2='<a href="/quote/dog?<a href="/quote/cat?<a href="/quote/monkey?<a href="/quote/human?'
words=re.findall("<a href=\"/quote/(.+?)?",g2)
And it returns words=['d', 'c', 'm', 'h']. Why isn't this working in this case?
Reply
#4
>>> words=re.findall(r'<a href="/quote/([^?]+)\?',g2)
>>> words
['dog', 'cat', 'monkey', 'human']
>>>
Reply
#5
Also if have real html and not a mess like this with some href trow in,then should use a parser.
from bs4 import BeautifulSoup

html = '''\
<div class='animals'>
  <a href="https://en.wikipedia.org/wiki/Dog">dog</a>
  <a href="https://en.wikipedia.org/wiki/Cat">cat</a>
</div>'''

soup = BeautifulSoup(html, 'lxml')
print([tag.text for tag in soup.find_all('a')])
Output:
['dog', 'cat']
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Question on HTML formatting with set string in message Cknutson575 3 241 Mar-09-2021, 08:11 AM
Last Post: Cknutson575
Question How to extract multiple text from a string? chatguy 2 230 Feb-28-2021, 07:39 AM
Last Post: bowlofred
  How to print string multiple times on new line ace19887 7 630 Sep-30-2020, 02:53 PM
Last Post: buran
  convert string into multiple columns in python3 VAN 2 568 Sep-26-2020, 11:14 PM
Last Post: scidam
  Add new line after finding last string in a region Nigel11 1 440 Aug-08-2020, 10:00 PM
Last Post: Larz60+
  Finding line numbers starting with certain string Sutsro 3 618 Jun-27-2020, 12:36 PM
Last Post: Yoriz
  How to print string multiple times separated by delimiter Mekala 1 518 May-23-2020, 09:21 AM
Last Post: Yoriz
  Split string with multiple delimiters and keep the string in "groups" DreamingInsanity 4 1,135 May-12-2020, 09:31 AM
Last Post: DeaD_EyE
  Problem with accepting multiple string inputs Ryan_Todd 5 811 Jan-22-2020, 06:12 PM
Last Post: buran
  Split a long string into other strings with no delimiters/characters krewlaz 4 768 Nov-15-2019, 02:48 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020