HELP/QUES: regexp and strings 
Author Message
 HELP/QUES: regexp and strings

Hi there,

I wonder if any of you could help me out with a string manipulation
prolem I'm having, by way of code or helpful pointers?  What I'm wanting
to do is this:

I have a string such as '$phrase="this is a book"', and what I'd like to
do is wrap an html tag around it such as '<span id="bk">this is a
book</span>'.  The only problem is that I'm reading lines from an HTML
file, and whereas I'd like to wrap that tag around any text, I don't
want it going inside any tags already in the line, such as '<img
src="book1.gif" border=0 alt="<span...>this is a book</span> cover"
width=10 heigth=20>' would be a very bad thing.

I know how to substitute strings as in:
'$line=~s/$phrase/<span..>$phrase<\/span>/ig;' but I don't know how to
check to see if that $phrase is in a tag or not, and still output the
full $line (tags and all).

For example, the input line might be:

<td align="center"><img src="1.gif" alt="a book">This is a book.  And in
a book like this there are words.</td>

the $phrase="a book" so the output should be:

<td align="center"><img src="1.gif" alt="a book">This is <span id="bk">a
book</span>.  And in <span id="bk">a book</span> like this there are
words.</td>

If you can help at all I would really appreciate it.  Thanks very much!

Andy



Tue, 15 May 2001 03:00:00 GMT  
 HELP/QUES: regexp and strings

Quote:

> The only problem is that I'm reading lines from an HTML file, and
> whereas I'd like to wrap that tag around any text, I don't want it
> going inside any tags already in the line,

So, you need to parse the HTML. Try HTML::Parser from CPAN. Hope this
helps!

--
Tom Phoenix       Perl Training and Hacking       Esperanto
Randal Schwartz Case:     http://www.rahul.net/jeffrey/ovs/



Wed, 16 May 2001 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. regexp ques. re: /$var/

2. Newbie ques: How to concatenate two strings?

3. HELP: deactivating regexp-active chars from string?

4. Help: bol regexp in split string

5. Extracting strings from postscript in a single regexp?

6. Regexp multiple string matches

7. Using a string for a regexp pattern

8. The regexp [^string]

9. How to extract the toplevel-domain of a domainame from a string with regexp

10. negating a string in a regexp

11. searching for arbitrary literal string as opposed to regexp

12. Regexp to match a C-style string

 

 
Powered by phpBB® Forum Software