[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

[FWP] using (?> ... )



I was recently confronted with a puzzling need on DALnet #perl.  Someone
wanted a regex that would insert a "<br>" into a long URL under these
conditions:

  1 to 28 characters followed by a / or .
  OR
  1 to 28 characters

The "<br>" would be inserted after this pattern.  My first effort was:

  1 while $URL =~ s{
    (.* (?= <br> ))
    (
      .{1,28} [./]
      |
      .{1,28}
    )
    (?= . )
  }{$1$2<br>}x;

This didn't work totally.  I found out two things:

  1. m/.*(?=<br>))/ is not working how I'd like -- I wanted it to mean
     "match everything up to that which is followed by <br>" but it does
     less than that so that the entire pattern works.  I'd have to use
     (?> ... ).  I liked that idea. :)

  2. m!.{1,28}[./]?! is NOT the same as m!.{1,28}[./]|.{1,28}!, which I
     found out by using -Mre=debug with my program.  It turns out I want
     to use the latter regex, which I am doing, but I was curious about
     the seemingly similar patterns.

Oh, and you'll notice I do (?=<br>), so that the <br> is included in the
28 characters -- this is in accordance to what I was asked to do.

I also removed the (?=.) test, and changed the regex to

  s{
    ((?> .* (?= <br> ) ))
    (?! .{1,28} $)
    (
      .{1,28} [./]
      |
      .{1,28}
    )
  }{$1$2<br>}x;

The regex is at http://www.pobox.com/~japhy/regexes/format_long_string

-- 
Jeff "japhy" Pinyan     japhy@pobox.com     http://www.pobox.com/~japhy/
PerlMonth - An Online Perl Magazine            http://www.perlmonth.com/
The Perl Archive - Articles, Forums, etc.    http://www.perlarchive.com/
CPAN - #1 Perl Resource  (my id:  PINYAN)        http://search.cpan.org/


==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe