Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: text extraction question

by wulvrine (Friar)
on Dec 05, 2006 at 19:24 UTC ( [id://587958]=note: print w/replies, xml ) Need Help??


in reply to text extraction question

echoangel911
The simplest way would be to use a regular expression, for example
/w(.+)b(.+)cm(.+)sw(\S*)\s*$/
Which would mean find anything (not assuming digits) after the w, the b, the cm, and the sw.
The final (\S)\s*$ points to any non white space(\S) followed by any whitespace(\s*), followed by end of line ($).
This will take anything after the 'sw' tag that ISNT white space but would leave any extra spacing (space/tabs etc) at the end of the line out of the match. The matches themselves are stored in the variables $1 thru $4.
Here is an example

#! /usr/bin/perl use strict; use warnings; my $templateformat = 'w<NM>b<NM>cm<CH>sw<SW>'; my $inputexample = 'w8b8cm512swno'; if ($templateformat =~ /w(.+)b(.+)cm(.+)sw(\S*)\s*$/ ) { print "template found\n"; my $first = $1; my $second = $2; my $third = $3; my $fourth = $4; print "first=$first, second=$second, third=$third, fourth=$fourth\n" +; } if ($inputexample =~ /w(.+)b(.+)cm(.+)sw(\S*)\s*$/ ) { print "input found\n"; my $first = $1; my $second = $2; my $third = $3; my $fourth = $4; print "first=$first, second=$second, third=$third, fourth=$fourth\n" +; }

Output is:

template found
first=<NM>, second=<NM>, third=<CH>, fourth=<SW>
input found
first=8, second=8, third=512, fourth=no

I hope that helps!

s&&VALKYRIE &&& print $_^q|!4 =+;' *|

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://587958]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-03-28 15:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found