Pattern matching and replacing in Python

2024/9/20 13:57:40

I'm trying to take a string that can be anything like "Hello here is a [URL]www.url.com[/URL] and it's great." and be able to extract whatever is between [URL] and [/URL] and then modify the string so it now becomes "Hello here is a < a href='www.url.com'>www.url.com< /a> and it's great."

So I was thinking maybe I could do a string.split() and then get the text between [URL] and [/URL] and then do a string.replace() with the new part but I wanted to know if there was a simpler solution.

Answer

Use regular expressions:

import re
print re.sub(r'\[URL\](.*?)\[/URL\]',r'<a href="\1">\1</a>', "Hello here is a [URL]www.url.com[/URL] and it's great.")
https://en.xdnf.cn/q/119324.html

Related Q&A

In Python word search, searching diagonally, printing result of where word starts and ends

I have a friend of mine tutoring me in learning Python and he gave me this project where a user will read a word search into the program and the file includes a list of words that will be in the word s…

Selenium python : element not interactable

I am trying to scrape information from this website example website I need to get the version 2021 and search by code. Here is my code: from selenium import webdriver from selenium.webdriver.chrome.opt…

Date into matplotlib graph

How can I use a date from a Sqlite database on the x-axis to make a bar graph with matplotlib?If I convert the date to unix timestamp the graph works, but I would like to get something like this: http…

Non blocking IO - Programming model

In non blocking IO programming model, a thread blocked on data available channels, as shown below, in python,while True:readers, _, _ = select.select([sys.stdin, sock], [], []) # blocked select()for re…

How to globally change xticks label relplot in Seaborn

I am facing an issue to globally changed the x-tick label for plot render using the relplot The idea was to change the int to string x-tick label for both plot. The desired label is [a,b,c,d] However, …

How to solve MatplotlibDeprecationWarning: scipy.stats.norm.pdf warning?

I am using matplotlib in my Python code. I got following warning: xxx.py:88: MatplotlibDeprecationWarning: scipy.stats.norm.pdfy = 100 * mlab.normpdf(bin_middles, mu, sigma)*bin_width I was wondering…

time data 42:53.700 does not match format %H:%M:%S.%f (match)

I am trying to convert a column in string format to DateTime format, However, I am getting the following error, could somebody please help? The error:time data 42:53.700 does not match format %H:%M:%S…

How can I import .py file? [duplicate]

This question already has answers here:Adding a directory to sys.path with pathlib(4 answers)Closed last year.Below is my code: from pathlib import Path import os import sys sys.path.insert(0, os.path.…

python: convenient way to create list of strings of repeated elements

How can I create a list like this:["a","a","a",... (repeating "a" a hundred times") "b","b","b",(repeating "b" a hun…

An accurate python sleep function

Ive tried time.sleep(), but its accuracy is total garbage. Consider this loop, for instance:for i in range(10000000):print(i)sleep(.334)Watch the numbers it prints. If its anything like my computer, it…