I'm using regular expressions to parse HTML hyperlinks and I've run into a problem. I'm trying to escape characters
such as '.' and '?' for use in regular expressions, but it's not working -
# Grabs a link. For this example, let's say that the string grabbed is '<a href="http://google.com/?q=foo">Click</a>'
-
link_url_original = GetLink()
-
-
# Sanitize string for regex use
-
link_url_original = re.sub("\.", "\.", link_url_original)
-
link_url_original = re.sub("\?", "\?", link_url_original)
-
-
toSub = 'http://google.com/?q=foo'
-
to Repl = 'http://www.yahoo.com'
-
-
final = re.sub(toSub, toRpl, link_url_original)
-
print final
-
The output is: - <a href="http://google\.com/\?q=foo">Click</a>
Why aren't the added slashes being interpretted as escape characters?
4 1347
The regular expression is the match string, not the replace string. The correct syntax would be like - result = re.sub("\.", ".", subject)
The regular expression is the match string, not the replace string. The correct syntax would be like - result = re.sub("\.", ".", subject)
Replace a "." with another "."? What???
Ahh, sorry. I didn't understand what you were trying to do.
The answer is that the toSub needs to be escaped, not the link_original_url... -
import re
-
#link_url_original = GetLink()
-
link_url_original = '<a href="http://google.com/?q=foo">Click</a>'
-
-
toSub = "http://google.com/?q=foo"
-
# Sanitize string for regex use
-
toSub = re.sub("\.", "\.", toSub)
-
toSub = re.sub("\?", "\?", toSub)
-
toRpl = "http://www.yahoo.com"
-
-
final = re.sub(toSub, toRpl, link_url_original)
-
print final
-
The result is...
<a href="http://www.yahoo.com">Click</a>
Ah, I can't believe I didn't catch that, thanks.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Henry |
last post by:
I have this simple code,
string escaped = Regex.Escape( @"`~!@#$%^&*()_=+{}\|;:',<.>/?" + "\"" );
string input = @"a&+" + "\"" + @"@(-d)\e";
Regex re = new Regex( string.Format(@"(+)", escaped),...
|
by: stoppal |
last post by:
need to extract all text between the following strings, but not
include the strings.
"<!-- #BeginEditable "Title name" -->"
"<p align="center">#### </p>"
I am using preg_match(????, $s,...
|
by: aevans1108 |
last post by:
expanding this message to microsoft.public.dotnet.xml
Greetings
Please direct me to the right group if this is an inappropriate place
to post this question. Thanks.
I want to format a...
|
by: Theo Chakkapark |
last post by:
I'm having issues trying to replace text with PHP.
For example, if I have a string of text that reads:
{tag}
And want to replace that with:
$_POST
|
by: bill tie |
last post by:
I'd appreciate it if you could advise.
1. How do I replace "\" (backslash) with anything?
2. Suppose I want to replace
(a) every occurrence of characters "a", "b", "c", "d" with "x",
(b)...
|
by: clintonG |
last post by:
I'm using an .aspx tool I found at but as nice as the interface is I
think I need to consider using others. Some can generate C# I understand.
Your preferences please...
<%= Clinton Gallagher
...
|
by: lgbjr |
last post by:
Hi All,
I'm trying to split a string on every character. The string happens to be a
representation of a hex number. So, my regex expression is ().
Seems simple, but for some reason, I'm not...
|
by: Geoff Caplan |
last post by:
Hi folks,
The thread on injection attacks was very instructive, but seemed to
run out of steam at an interesting point. Now you guys have kindly
educated me about the real nature of the issues,...
|
by: placid |
last post by:
Hi All,
I have these files; which are Merge Request (ClearCase) files that are
created by a Perl CGI script (being re-written in Python, as the HTML/
JavaScript have been mixed with Perl,...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
| |