regex expression help

T

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.
 
M

Martijn Lievaart

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
 
G

Gunnar Hjalmarsson

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.}

A backreference might be useful.

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

RegEx 0
Tasks 1
Requesting regular expression help 12
Help with code plsss 0
Homework in C - Help Needed 1
Help with dynamic regex 14
Regex help 11
Need help with regex expression 6

Members online

Forum statistics

Threads
474,208
Messages
2,571,079
Members
47,682
Latest member
TrudiConna

Latest Threads

Top