regexp problem

Kevin Buettner plug-discuss@lists.plug.phoenix.az.us
Wed, 11 Dec 2002 12:57:08 -0700


On Dec 11, 12:30pm, Lynn David Newton wrote:

> I need a (Perl) regular expression that lops the first
> word off a string which could possibly (does, in fact)
> include newlines.
> 
> In the problem in question, the string data should only
> contain digits. What is happening is that something is
> causing a string to look like this: "234\n234", a
> duplicate of its original self, with a newline
> inserted, and because it's necessarily a string for
> various operational reasons, that's how it's being
> written to a database.
> 
> I found this solution myself, a two-step operation.
> Given that $id="234\n234":
> 
> $id =~ s/\D/ /g;
> $id =~ s/\D.*$//g;
> 
> Works (I think).
> 
> The first line changes non-digit characters to spaces,
> giving me an ordinary space-delimited string.
> 
> The second line swacks off everything at the end
> starting from the first non-digit character.
> 
> In the other solutions I tried, I couldn't deal with
> the newline.
> 
> Maybe someone can see a simpler solution. 

Here are a couple of ideas:

    ($id) = ($id =~ m/(\d+)/);

Or perhaps:

    ($id) = ($id =~ m/(\S+)/);

Or, if you really want to do a substition...

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

Kevin