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

I have working regex's which substitute errant fonts in Framemaker .mif files to something more agreeable to our tech writers. ie.
my $pattern = qr{<PgfTag `Normal \(T\)'>\s*\n\s*<ParaLine\s*\n\s*<Stri +ng `$oldLabel'>\s*\n}; my $replacement = "<PgfTag `Block Label \(T\)'>\n<ParaLine\n<String `$ +newLabel'>\n"; my $k = 0; $$contents =~ s/$pattern/++$k && $replacement/ge;
What I would like to do is reformat & comment these regex's more in the style discussed in perlfaq6. Yet when I break the qr// statement up into multiple lines, I no longer match correctly. Is there a way to do this?

Thanks for any insight you can pass along!

j

Replies are listed 'Best First'.
Re: commenting regular expressions?
by herveus (Prior) on Sep 04, 2003 at 18:02 UTC
    Howdy!

    Did you use the /x modifier?

    I see white space in your regexes. If you use /x, you have to escape the white space or it gets ignored.

    For example (untested):

    qr{<PgfTag [ ] # this matches a space 'Normal \ # the backslash ought to escape the space \\(T\)'> ...
    and so forth...

    yours,
    Michael

      I had played with the following to no avail:
      #!/usr/bin/perl my $s = "blah,blah,blah <PgfTag `Normal \(T\)'> yadda,yadda,yadda"; print "s =\t\"$s\"\n"; my $pattern = qr{ \s* <PgfTag `Normal \(T\)'> \s* }; my $k = 0; $s =~ s/$pattern/++$k && ','/gex; print "$k change(s)\n"; print "s =\t\"$s\"\n";
        You have to put /x on the expression when you declare it. Also, as said, escape the literal spaces in the tags, or use \s if you don't mind matching any whitespace.
        my $pattern = qr{ <PgfTag\ `Normal\ \(T\)'> \s* \n \s* <ParaLine \s* \n \s* <String\ `$oldLabel'> \s* \n }x;
Re: commenting regular expressions?
by ajdelore (Pilgrim) on Sep 04, 2003 at 17:58 UTC

    You need to add the /x modifier to your regexes.

    Updated: as pointed out by a couple of others, you need to add it to each qr{...}x statement, not to the s//.

    $$contents =~ s/$pattern/++$k && $replacement/gex;

    </ajdelore>

Re: commenting regular expressions?
by tcf22 (Priest) on Sep 04, 2003 at 17:59 UTC
    use the x modifier

    Update: Added example code
    my $pattern = qr/ \s* <PgfTag `Normal \(T\)'> \s* /x;