regexp problem

Lynn David Newton plug-discuss@lists.plug.phoenix.az.us
Wed, 11 Dec 2002 22:21:21 -0700


Thanks to all who responded to my regexp question.

  >> I think the RE symbol you missed was the ^ anchor.

  Kevin> My guess is that Lynn was having trouble
  Kevin> getting . to match \n. Using the 's' qualifier
  Kevin> enables this.

Kevin is right, I overlooked the s option to s///. I
automatically started with the ^ anchor. I wound up
using a solution using the $ rear end anchor instead.
It worked.

The discussion is now academic because in the project I
was working on, we decided to take a different route,
so I didn't need it, but it was still a usefule
exercise. Meanwhile, a better solution is this:

$id =~ s/^(\S+)\s.*/$1/s;

The RE matches any sequence of non-white-space
characters followed by any whitespace followed by
anything and substitutes just the first chunk of white
space.

I tested this using these two values:

$id = "246\n357\n\n68";
$id = "abc\ndef\n\ngh";

It could probably be simplified more, but I don't see
any flaws in this solution.

-- 
Lynn