473,396 Members | 2,037 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.

Regexp: right patternt for the following ...

Hi all,

I am looking for the right pattern to grab the following HTML text:

"<a href="http://www.genericdomain.com">Text Text Text</a>"

and to exclude the following:

"<a href="http://www.genericdomain.com"><img src=image.gif></a>"

At the moment, I use this pattern: "<a href=\"(.+?)\">(.+?)</a>" that
grabs both the previous strings.
Can you help me, please?

Many thanks.
Regards,
Stefania

Jul 17 '05 #1
8 1561
On 13 Sep 2004 06:05:24 -0700, "it************@hotmail.com"
<it************@hotmail.com> wrote:
Hi all,

I am looking for the right pattern to grab the following HTML text:

"<a href="http://www.genericdomain.com">Text Text Text</a>"

and to exclude the following:

"<a href="http://www.genericdomain.com"><img src=image.gif></a>"

At the moment, I use this pattern: "<a href=\"(.+?)\">(.+?)</a>" that
grabs both the previous strings.
Can you help me, please?

Many thanks.
Regards,
Stefania


Use an HTML parser... parsing HTML code with "simple regex" cannot be
done particularly reliably.. that's why parsers exist ;)

Regards,

Ian

--
Ian.H
digiServ Network
London, UK
http://digiserv.net/
Jul 17 '05 #2
it************@hotmail.com wrote:
Hi all,

I am looking for the right pattern to grab the following HTML text:

"<a href="http://www.genericdomain.com">Text Text Text</a>"

and to exclude the following:

"<a href="http://www.genericdomain.com"><img src=image.gif></a>"

At the moment, I use this pattern: "<a href=\"(.+?)\">(.+?)</a>" that
grabs both the previous strings.
Can you help me, please?


I think what you'd want to do here is to use a negative look-ahead...
but if you plan on parsing more HTML than this, consider using some kind
of parser.

<?php
$strs=array(
'<a href="http://www.genericdomain.com">Text Text Text</a>',
'<a href="http://www.genericdomain.com"><img src=image.gif></a>'
);
foreach($strs as $str){

preg_match('`<a\s+href=\"([^"]*)\">(?!<img[^>]*>)(.*)</a>`i',$str,$matches);
print_r($matches);
}
?>

--
Justin Koivisto - sp**@koivi.com
http://www.koivi.com
Jul 17 '05 #3
it************@hotmail.com wrote:
I am looking for the right pattern to grab the following HTML text:

"<a href="http://www.genericdomain.com">Text Text Text</a>"

and to exclude the following:

"<a href="http://www.genericdomain.com"><img src=image.gif></a>"

At the moment, I use this pattern: "<a href=\"(.+?)\">(.+?)</a>" that
grabs both the previous strings.


Tested:
<a href=\"(.+)\">([^<]*)</a>

Matthias
Jul 17 '05 #4
Matthias Esken wrote:
it************@hotmail.com wrote:

I am looking for the right pattern to grab the following HTML text:

"<a href="http://www.genericdomain.com">Text Text Text</a>"

and to exclude the following:

"<a href="http://www.genericdomain.com"><img src=image.gif></a>"

At the moment, I use this pattern: "<a href=\"(.+?)\">(.+?)</a>" that
grabs both the previous strings.

Tested:
<a href=\"(.+)\">([^<]*)</a>


Just an FYI... these wouldn't match the above pattern:

<a href="here.html">Bet you can't <b>find ME!</b></a>

<a href="here.html">Bears < Elephants</a>

--
Justin Koivisto - sp**@koivi.com
http://www.koivi.com
Jul 17 '05 #5
"Justin Koivisto" <sp**@koivi.com> wrote in message
news:1r*****************@news7.onvoy.net...
Matthias Esken wrote:
it************@hotmail.com wrote:

I am looking for the right pattern to grab the following HTML text:

"<a href="http://www.genericdomain.com">Text Text Text</a>"

and to exclude the following:

"<a href="http://www.genericdomain.com"><img src=image.gif></a>"

At the moment, I use this pattern: "<a href=\"(.+?)\">(.+?)</a>" that
grabs both the previous strings.

Tested:
<a href=\"(.+)\">([^<]*)</a>


Just an FYI... these wouldn't match the above pattern:

<a href="here.html">Bet you can't <b>find ME!</b></a>

<a href="here.html">Bears < Elephants</a>

--
Justin Koivisto - sp**@koivi.com
http://www.koivi.com


maybe this then? (untested): <a href=\"(.+)\">(^[^<]*)</a>
Jul 17 '05 #6
kingofkolt wrote:
"Justin Koivisto" <sp**@koivi.com> wrote in message
news:1r*****************@news7.onvoy.net...
Matthias Esken wrote:
it************@hotmail.com wrote:

I am looking for the right pattern to grab the following HTML text:

"<a href="http://www.genericdomain.com">Text Text Text</a>"

and to exclude the following:

"<a href="http://www.genericdomain.com"><img src=image.gif></a>"

At the moment, I use this pattern: "<a href=\"(.+?)\">(.+?)</a>" that
grabs both the previous strings.
Tested:
<a href=\"(.+)\">([^<]*)</a>


Just an FYI... these wouldn't match the above pattern:

<a href="here.html">Bet you can't <b>find ME!</b></a>

<a href="here.html">Bears < Elephants</a>

--
Justin Koivisto - sp**@koivi.com
http://www.koivi.com

maybe this then? (untested): <a href=\"(.+)\">(^[^<]*)</a>


Or the one I posted about 6 hours ago... (which was tested)

<a\s+href=\"([^"]*)\">(?!<img[^>]*>)(.*)</a>`i

;)

--
Justin Koivisto - sp**@koivi.com
http://www.koivi.com
Jul 17 '05 #7
Justin Koivisto wrote:
Matthias Esken wrote:
<a href=\"(.+)\">([^<]*)</a>
Just an FYI... these wouldn't match the above pattern:

<a href="here.html">Bet you can't <b>find ME!</b></a>


This is correct, but the example did not look like that.
<a href="here.html">Bears < Elephants</a>


This is invalid HTML. ;-)

Regards,
Matthias
Jul 17 '05 #8
Matthias Esken wrote:
Justin Koivisto wrote:

<a href="here.html">Bears < Elephants</a>


This is invalid HTML. ;-)


A timely demonstration of why HTML parsers are used.

--
Jock
Jul 17 '05 #9

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

Similar topics

5
by: Ronald Fischer | last post by:
I have a server-side JavaScript function returning a string. I would like to test wheather or not the string contains the following pattern: - an equal sign, - followed by one or more characters...
26
by: rkleiner | last post by:
Is there a regular expression to find the first unmatched right bracket in a string, if there is one? For example, "(1*(2+3))))".search(/regexp/) == 9 Thanks in advance.
20
by: RobG | last post by:
I'm messing with getPropertyValue (Mozilla et al) and currentStyle (IE) and have a general function (slightly modified from one originally posted by Steve van Dongen) for getting style properties:...
2
by: Bill McCormick | last post by:
Hello, I'm new to VB.NET but have used regexp in Perl and VI. I'd like to read a regular expression from a file and apply it to a string read from another file. The regexp is simple word...
7
by: iannorton | last post by:
Hi, I'm trying to write a RegExp that will return search an array and return the closest 10 matches, for example, if i entered "Hel" and my array contained "Hello", "Hell", it would return Hello...
7
by: Csaba Gabor | last post by:
I need to come up with a function function regExpPos (text, re, parenNum) { ... } that will return the position within text of RegExp.$parenNum if there is a match, and -1 otherwise. For...
3
by: Russell | last post by:
hey, I'm struggling trying to get the concepts of the regExp function down.... What i'm trying to achieve is to remove all white space from html formatted source code. I have the following...
27
by: SQL Learner | last post by:
Hi all, I have an Access db with two large tables - 3,100,000 (tblA) and 7,000 (tblB) records. I created a select query using Inner Join by partial matching two fields (X from tblA and Y from...
5
by: Ryan Krauss | last post by:
I need to parse the following string: $$\pmatrix{{\it x_2}\cr 0\cr 1\cr }=\pmatrix{\left({{{\it m_2}\,s^2 }\over{k}}+1\right)\,{\it x_1}-{{F}\over{k}}\cr -{{{\it m_2}\,s^2\,F...
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
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
Oralloy
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,...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
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,...
0
isladogs
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 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.