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
  String to List question help James_Thomas 6 921 Sep-06-2023, 02:32 PM
Last Post: deanhystad
  Trying to understand strings and lists of strings Konstantin23 2 699 Aug-06-2023, 11:42 AM
Last Post: deanhystad
  splitting file into multiple files by searching for string AlphaInc 2 816 Jul-01-2023, 10:35 PM
Last Post: Pedroski55
  xml indent SubElements (wrapping) with multiple strings ctrldan 2 1,383 Jun-09-2023, 08:42 PM
Last Post: ctrldan
  syntax error question - string mgallotti 5 1,251 Feb-03-2023, 05:10 PM
Last Post: mgallotti
  Finding First Digits in String giddyhead 4 1,323 Aug-17-2022, 08:12 PM
Last Post: giddyhead
Question Finding string in list item jesse68 8 1,801 Jun-30-2022, 08:27 AM
Last Post: Gribouillis
  Matching multiple parts in string fozz 31 6,060 Jun-13-2022, 09:38 AM
Last Post: fozz
  Splitting strings in list of strings jesse68 3 1,703 Mar-02-2022, 05:15 PM
Last Post: DeaD_EyE
  Search multiple CSV files for a string or strings cubangt 7 7,842 Feb-23-2022, 12:53 AM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

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