CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
match html string <!-- this is a comment -->

 



augg05
New User

Oct 25, 2013, 1:35 PM

Post #1 of 3 (11087 views)
match html string <!-- this is a comment --> Can't Post

Hello,

I am trying to match just the string that is inside the html tags in the comment section of a html file (<!-- this is a comment -->) and replace it with caps. I found a way to match everything in the pattern, including the tags, but that is not what I am after.

This regex matches everything, but I am just trying to match the text "this is a comment" and replace it with caps. This is for a homework exercise so any help or tips will be appreciated. Thanks!

s/<.+?>//sg


BillKSmith
Veteran

Oct 25, 2013, 4:10 PM

Post #2 of 3 (11081 views)
Re: [augg05] match html string <!-- this is a comment --> [In reply to] Can't Post

You really have three problems:

  • Read your entire file into a single string


  • Write a regex that will match an html comment


  • Write a substitute command that will convert all html comments to upper case.


  • The first is best done with a CPAN module such as Slurp. In your case it is probably sufficient to undefine the INPUT_RECORD_SEPARATOR ($/ - Refer perldoc perlvar).

    Your regex has to match all the special characters that make up an html comment. This is a bit difficult because many of them are regex metacharacters. The easy solution is to use \Q...\E to escape all-non alpha characters. Be careful not to escape any characters that you intend to have the usual regex meaning.


    Your substitution command requires several modifiers (Refer to the substitution operator in perldoc perlop):

  • /m and /s for the multi-line string


  • /g because there can be more that one comment


  • /e to execute the uc function in the replacement field


  • /r (only if you do not wish to change the original string

  • Good Luck,
    Bill


    augg05
    New User

    Oct 25, 2013, 4:21 PM

    Post #3 of 3 (11079 views)
    Re: [BillKSmith] match html string <!-- this is a comment --> [In reply to] Can't Post

    Thanks Bill for your time and quick response. I will have some time to work on this over the weekend.

     
     


    Search for (options) Powered by Gossamer Forum v.1.2.0

    Web Applications & Managed Hosting Powered by Gossamer Threads
    Visit our Mailing List Archives