Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with regular expression please 2

Status
Not open for further replies.

janvdk

IS-IT--Management
Jan 15, 2005
8
BE
Dear all,

I need a little help with a regular expression.

For the following string $htmlline I'd like to replace ALL tags of format <A whatever text> by <A>.

Using the following, it only does it for the first tag it encounters
$htmlline =~ s/<A(.*?)>/<A>/;

$htmlline= <A href="/search.php3?publisherid=9">Publisher1</A><br><A href="/search.php3?publisherid=3">Publisher2</A><br><A href="/search.php3?publisherid=42">Publisher3</A></td>

Thanks for your feedbacks !

Jan
 
just add a 'g' modifier to your substition -
$htmlline =~ s/<A(.*?)>/<A>/g;

you might run into problems with this method though.. it would be better to use an html parser module

and check out this perlmonks article.. I don't use .*? as much as I used to after reading this

 
Ecellent atical chaziod. I use .*? all the time. What would be a better way to write this?

Code:
if (/^(\d{1,2}\.)(.*?)$/) {				# Look for 1 or 2 digits followed by a "." = $1 everything else to end of line = $2
 
That code looks fine, except you could leave out the ? if you want.
 
As Tony says, you could (actually should) leave out the ? there, since it serves no purpose if you want to match everything else until the end.

I rarely use {n1,n2} quantifiers, unless it's something that can't easily be counted by eye. I believe I've read that it's also more efficient to "spell it out." (I think I read this in Jeffrey Friedl's Mastering Regular Expressions, though I can't cite chapter and verse.)

I'd probably write the expression above as
if (/^(\d\d?\.)(.*)$/)

where \d\d? means "a digit followed by zero or one digits."

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top