473,396 Members | 1,996 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

regular expression replace src attribute in image tag

hi all, i've to replace the src tag in a html file with regex,

images tags look like this :

<td valign="top" align="middle" width="74"><img height="40"
src="1_interr.gif" alt="1_interr.gif" width="40" /></td>

what i want to do is to match form <img.... to... /but backtrack
only 1_interr.gif

i tried using lookbehind and lookahead assertions with this pattern

(?<=<img.*?src=.*?")(?<src>([^"]*?))(?=".*?>)

but i returns 3 matches :

1_interr.gif
width=
40

i need only first one

what's wrong ?

thanks.

Sep 26 '07 #1
3 4552
FabSW wrote:
hi all, i've to replace the src tag in a html file with regex,

images tags look like this :

<td valign="top" align="middle" width="74"><img height="40"
src="1_interr.gif" alt="1_interr.gif" width="40" /></td>

what i want to do is to match form <img.... to... /but backtrack
only 1_interr.gif

i tried using lookbehind and lookahead assertions with this pattern

(?<=<img.*?src=.*?")(?<src>([^"]*?))(?=".*?>)

but i returns 3 matches :

1_interr.gif
width=
40
Funny. I would expect it to return 5 matches:

1_interr.gif
alt=
1_interr.gif
width=
40
i need only first one

what's wrong ?

thanks.
In the lookbehind you are allowing anything between src= and the
quotation mark. Just change src=.*?" to src="

--
Göran Andersson
_____
http://www.guffa.com
Sep 26 '07 #2
On 26 Set, 18:35, Göran Andersson <gu...@guffa.comwrote:
FabSW wrote:
hi all, i've to replace the src tag in a html file with regex,
images tags look like this :
<td valign="top" align="middle" width="74"><img height="40"
src="1_interr.gif" alt="1_interr.gif" width="40" /></td>
what i want to do is to match form <img.... to... /but backtrack
only 1_interr.gif
i tried using lookbehind and lookahead assertions with this pattern
(?<=<img.*?src=.*?")(?<src>([^"]*?))(?=".*?>)
but i returns 3 matches :
1_interr.gif
width=
40

Funny. I would expect it to return 5 matches:

1_interr.gif
alt=
1_interr.gif
width=
40
i need only first one
what's wrong ?
thanks.

In the lookbehind you are allowing anything between src= and the
quotation mark. Just change src=.*?" to src="
Thanks it work almost in any file,
but it don't when there is white spaces or form feed

eg.

<i
mg .....

or

src
"

what's the way ?

thanks again.

Sep 27 '07 #3
FabSW wrote:
On 26 Set, 18:35, Göran Andersson <gu...@guffa.comwrote:
>FabSW wrote:
>>hi all, i've to replace the src tag in a html file with regex,
images tags look like this :
<td valign="top" align="middle" width="74"><img height="40"
src="1_interr.gif" alt="1_interr.gif" width="40" /></td>
what i want to do is to match form <img.... to... /but backtrack
only 1_interr.gif
i tried using lookbehind and lookahead assertions with this pattern
(?<=<img.*?src=.*?")(?<src>([^"]*?))(?=".*?>)
but i returns 3 matches :
1_interr.gif
width=
40
Funny. I would expect it to return 5 matches:

1_interr.gif
alt=
1_interr.gif
width=
40
>>i need only first one
what's wrong ?
thanks.
In the lookbehind you are allowing anything between src= and the
quotation mark. Just change src=.*?" to src="

Thanks it work almost in any file,
but it don't when there is white spaces or form feed

eg.

<i
mg .....

or

src
"

what's the way ?

thanks again.
You can specify to allow any number of white spaces between the items:

src\s*=\s*"

--
Göran Andersson
_____
http://www.guffa.com
Sep 27 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Kenneth McDonald | last post by:
I'm working on the 0.8 release of my 'rex' module, and would appreciate feedback, suggestions, and criticism as I work towards finalizing the API and feature sets. rex is a module intended to make...
1
by: Markus Wallner | last post by:
Hi! I want to replace images through URLs with the help of a regular expression. For example: <img src='http://xxxxx.net/photo/small.jpg'> should be replaced through: <a...
11
by: Dimitris Georgakopuolos | last post by:
Hello, I have a text file that I load up to a string. The text includes certain expression like {firstName} or {userName} that I want to match and then replace with a new expression. However,...
8
by: P K | last post by:
I have an XML in which I have to comment out the <responseopt> tag the tags between this tag should not be commented I plan to use regular expressions The tags looks like this <responseopt...
7
by: Billa | last post by:
Hi, I am replaceing a big string using different regular expressions (see some example at the end of the message). The problem is whenever I apply a "replace" it makes a new copy of string and I...
5
by: ken.carlino | last post by:
Hi, I am new to python regular expression, I would like to use it to get an attribute of an html element from an html file? for example, I was able to read the html file using this: req =...
3
by: Rick Brandt | last post by:
I am trying to take a string of HTML content and replace some img elements. I have managed to come up with a Regular Expression that allows me to replace ALL img elements, but I need to limit the...
1
by: Matthias S. | last post by:
Hi there, I'm trying to build a regular expression which will do a replace for me. I'm getting grey hair on this one: here is my input string: <a href="http://test.com/fun.jpg"...
1
by: NvrBst | last post by:
I want to use the .replace() method with the regular expression /^ %VAR % =,($|&)/. The following DOESN'T replace the "^default.aspx=,($|&)" regular expression with "":...
2
by: Stimp | last post by:
Hi all, I'm having trouble trying to figure out the best approach to this problem and I believe regular expressions are the way to go. Basically I have created a large string of XHTML using a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
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
0
BarryA
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.