Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

regular expression for the address

by nandymamith (Initiate)
on Aug 29, 2012 at 10:01 UTC ( #990423=perlquestion: print w/ replies, xml ) Need Help??
nandymamith has asked for the wisdom of the Perl Monks concerning the following question:

Can anybody help me out in getting the value 195.16 from the below address :

 my $var = "10,0490152,"DORIAN'S TIJUANA, SA DE CV ",CALLE (ART 123) 1620-A ,TIJUANA BC ,MEXICO, ,OA617069,11/10/08,NA, (195.16),001111";

Comment on regular expression for the address
Download Code
Re: regular expression for the address
by choroba (Abbot) on Aug 29, 2012 at 10:17 UTC
    If the value will always be inside the parentheses and followed by a comma and series of numbers that end the string, you can use
    my ($value) = $var =~ /\(([0-9]+\.[0-9]+)\),[0-9]+$/;
    Explanation can be obtained vie YAPE::Regex::Explain:
    The regular expression: (?-imsx:\(([0-9]+\.[0-9]+)\),[0-9]+$) matches as follows: NODE EXPLANATION ---------------------------------------------------------------------- (?-imsx: group, but do not capture (case-sensitive) (with ^ and $ matching normally) (with . not matching \n) (matching whitespace and # normally): ---------------------------------------------------------------------- \( '(' ---------------------------------------------------------------------- ( group and capture to \1: ---------------------------------------------------------------------- [0-9]+ any character of: '0' to '9' (1 or more times (matching the most amount possible)) ---------------------------------------------------------------------- \. '.' ---------------------------------------------------------------------- [0-9]+ any character of: '0' to '9' (1 or more times (matching the most amount possible)) ---------------------------------------------------------------------- ) end of \1 ---------------------------------------------------------------------- \) ')' ---------------------------------------------------------------------- , ',' ---------------------------------------------------------------------- [0-9]+ any character of: '0' to '9' (1 or more times (matching the most amount possible)) ---------------------------------------------------------------------- $ before an optional \n, and the end of the string ---------------------------------------------------------------------- ) end of grouping ----------------------------------------------------------------------
    Note that your line is not valid, because it contains unescaped double quotes inside themselves.
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: regular expression for the address
by RichardK (Priest) on Aug 29, 2012 at 10:20 UTC

    that depends :)

    What makes (195.16) unique?

    Do you have more example text ?

    Can you describe your problem in detail?

Re: regular expression for the address
by Monk::Thomas (Beadle) on Aug 29, 2012 at 10:20 UTC

    As a first step I would replace the quoted fields with some placeholder. (Make sure to use a non-greedy regexp.) Afterwards you can do a simple '$nice_value = (split q{,}, $var)[11]'.

    This seems to be some CSV-like record. Why not use Text::CSV or some other parser module and let it worry about the quoting/escaping?

    code is untested, written from the top of my head

Re: regular expression for the address
by philiprbrenan (Monk) on Aug 29, 2012 at 13:11 UTC

    May I urge you to redact possibly live data? Thanks.

    use feature ":5.14"; use warnings FATAL => qw(all); use strict; my $var = << 'END'; 11,2222222,"xxxxxx'x xxxxxxx, xxxxxxxxx",sssss (aaa 111) 1111-a ,aaaaa +aa aa ,aaaaaa, ,xxxxxxxx,dd/dd/dd,xx, (195.16),111111 END say eval [split /,\s*/, $var]->[-2]

    Produces

    195.16
    

      What do you use that eval for? Only for eliminating the parentheses? Bad idea, I think.

      Let's assume there is a list of strings from which the value of the second last field is wanted.

      If there is a bad data source, that eval would execute bad stuff like system("echo Hello World.") or even worse...

      $ perl use feature ":5.14"; use warnings FATAL => qw(all); use strict; my $var = << 'END'; 11,2222222,"xxxxxx'x xxxxxxx, xxxxxxxxx",sssss (aaa 111) 1111-a ,aaaaa +aa aa ,aaaaaa, ,xxxxxxxx,dd/dd/dd,xx, system('echo bad command execut +ed!!'),111111 END say eval [split /,\s*/, $var]->[-2] bad command executed!! 0
      If you want to remove the parentheses, you could use an tr///. For example:
      use feature ":5.14"; use warnings FATAL => qw(all); use strict; my $var = << 'END'; 11,2222222,"xxxxxx'x xxxxxxx, xxxxxxxxx",sssss (aaa 111) 1111-a ,aaaaa +aa aa ,aaaaaa, ,xxxxxxxx,dd/dd/dd,xx, (195.14) ,111111 END my $interest = split /,\s*/, $var)[-2]; $interest =~ tr/0-9.//cd; say $interest;
Re: regular expression for the address
by philiprbrenan (Monk) on Aug 29, 2012 at 13:17 UTC

    May I urge you not to publish live data? Thanks.

    use feature ":5.14"; use warnings FATAL => qw(all); use strict; my $var = << 'END'; 11,2222222,"xxxxxx'x xxxxxxx, xxxxxxxxx",sssss (aaa 111) 1111-a ,aaaaa +aa aa ,aaaaaa, ,xxxxxxxx,dd/dd/dd,xx, (195.16),111111 END say eval [split /,\s*/, $var]->[-2]

    Produces

    195.16
    

      Thank You all :) This is not a live data, actually i found in some website so raised this query

      OK Fine Thank you..!! :)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://990423]
Approved by chacham
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (8)
As of 2014-07-31 23:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (255 votes), past polls