regex expression help

regex expression help

am 18.12.2007 22:00:56 von Tom.E.Ward

I need to extract serial numbers from multiple text files. I have an
expression that works fairly well, but the problem is some of the
serial numbers are enclosed within quotes (both single and double) and
I can't figure out a good way to get rid of the closing quote. The
expression of have now is:

/($matchString\s*[\:\=\-]\s*[\'\"]?)(([\w]+$)|(([\w]+)([^\w] )))/

($matchString contains {"serial number", "sn", "s/n", "serial num",
etc.}

All the quotes are because I use $2 to extract the actual number. Any
help is appreciated.

Re: regex expression help

am 18.12.2007 22:20:22 von Martijn Lievaart

On Tue, 18 Dec 2007 13:00:56 -0800, Tom.E.Ward wrote:

> I need to extract serial numbers from multiple text files. I have an
> expression that works fairly well, but the problem is some of the serial
> numbers are enclosed within quotes (both single and double) and I can't
> figure out a good way to get rid of the closing quote. The expression of
> have now is:
>
> /($matchString\s*[\:\=\-]\s*[\'\"]?)(([\w]+$)|(([\w]+)([^\w] )))/
>
> ($matchString contains {"serial number", "sn", "s/n", "serial num",
> etc.}
>
> All the quotes are because I use $2 to extract the actual number. Any
> help is appreciated.

How about:

if (/($matchString\s*[\:\=\-])\s*(.*?)\s*$/) {
my $sn = $2;
$sn =~ s/^['"]//;
$sn =~ s/['"]$//;
# do stuff....
}

Not 100% the same as the opening quote is not captured in $1, but I
cannot imagine you want that.

Otherwise, how about:

/($matchString\s*[\:\=\-]\s*['"]?)(.*?)['"]?\s*$/

HTH,
M4