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
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?
You could use a regex.
>>> s = "djfk83dhfdog83748djfk83dhfcat83748djfk83dhfmonkey83748djfk83dhfhuman83748"
>>> import re
>>> re.findall(r"djfk83dhf(.+?)83748", s)
['dog', 'cat', 'monkey', 'human']
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?
>>> words=re.findall(r'<a href="/quote/([^?]+)\?',g2)
>>> words
['dog', 'cat', 'monkey', 'human']
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="">dog</a>
  <a href="">cat</a>

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

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to combine multiple rows of strings into one using pandas? shantanu97 1 171 Aug-22-2021, 05:26 AM
Last Post: klllmmm
  Replace String in multiple text-files [SOLVED] AlphaInc 5 534 Aug-08-2021, 04:59 PM
Last Post: Axel_Erfurt
  Question about change hex string to integer sting in the list (python 2.7) lzfneu 1 547 May-24-2021, 08:48 AM
Last Post: bowlofred
  Question on HTML formatting with set string in message Cknutson575 3 817 Mar-09-2021, 08:11 AM
Last Post: Cknutson575
Question How to extract multiple text from a string? chatguy 2 607 Feb-28-2021, 07:39 AM
Last Post: bowlofred
  How to print string multiple times on new line ace19887 7 1,696 Sep-30-2020, 02:53 PM
Last Post: buran
  convert string into multiple columns in python3 VAN 2 1,030 Sep-26-2020, 11:14 PM
Last Post: scidam
  Add new line after finding last string in a region Nigel11 1 647 Aug-08-2020, 10:00 PM
Last Post: Larz60+
  Finding line numbers starting with certain string Sutsro 3 962 Jun-27-2020, 12:36 PM
Last Post: Yoriz
  How to print string multiple times separated by delimiter Mekala 1 770 May-23-2020, 09:21 AM
Last Post: Yoriz

Forum Jump:

User Panel Messages

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