Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello.

I have the following:

if ($filename =~ m/^{5,}[A-Z]./) { $section_title = ($filename =~ s/[:]?/:</B>\n<BR><BR>/); $filename = ""; }

And it doesn't work. I want it to replace the colon it finds on lines that have the first five characters written in CAPS and replace it with the colon as well as some HTML.

I've also tried this:

if ($filename =~ m/^{5,}[A-Z]./) { $section_title = ($filename =~ s/?[:]./':</B>\n< BR>< B>\n'); $filename = ""; }

That doesn't work either.

I'm new to regular expressions have been searching for some helpful information on this problem all over the place. Even went so far as to try to use the Reg. Expression used in the Perl Cookbook for parsing text files by comma seperated data.

Anyone have any suggestions?

Replies are listed 'Best First'.
(Ovid) RE: Regular Expression Help
by Ovid (Cardinal) on Aug 11, 2000 at 20:53 UTC
    With all due respect to jlistf, there were a couple of problems with his solution. Here's one that's easy to understand:
    if ($filename =~ /^[A-Z]{5}/) { ($section_title = $filename) =~ s!:!:</B>\n<BR><BR>!; $filename = ""; }
    For $filename, we're just testing for the first five letters, so the {5,} syntax just makes the regex engine do more work.

    For the actual substitution, the slash in </B> needs to be escaped unless we choose different delimiters like I have above.

    Also, we can't do

    $section_title = ($filename =~ s/?[:]./':</B>\n< BR>< B>\n');
    because the substitution returns a true/false value and if successful, $section_title receives a value of 1. The assignment to $section_title needs to be on a different line or assigned to as above (that's from a suggestion by KM).

    Cheers,
    Ovid

    Update: Cleaned up the code in response to KM's point.

      You can do:

      ($section_title = $filename) =~ s!:!:</B>\n<BR><BR>!;

      This will set $section_title to the substituted value of $filename, and leave $filename unmodified.

      Cheers,
      KM

Re: Regular Expression Help
by coreolyn (Parson) on Aug 11, 2000 at 21:18 UTC

    As for learning Regex's the "Learning Perl" (a.k.a. the llama) Is probably the best at getting the general gist of Regex's in perl. But if you really want to attempt to understand them "Mastering Regular Expressions"(a.k.a The owl) is what you are looking for. Be prepared to learn MORE than you want to know if you go that route. I've read it twice now and I'm still grappling with it. Not that it isn't good. It's packed with great information. The subject matter is just hard to grep. Eewww bad pun . . .:)

    coreolyn Duct tape devotee.

RE: Regular Expression Help
by jlistf (Monk) on Aug 11, 2000 at 20:40 UTC
    try:
    if ($filename =~ m/^[A-Z]{5,}/) { $section_title = ($filename =~ s/:/:</B>\n<BR><BR>/); $filename = ""; }
    the {5,} quantifier has to come after the [A-Z]. i also cleaned up the substitution regex a little.

    jeff

    Update: check out the other answers. they're better. thats what i get for posting too quickly, doh!
      Also, the </B> should be <\/B>