Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

regular expression

by rocketperl (Sexton)
on Jun 28, 2013 at 11:11 UTC ( #1041219=perlquestion: print w/ replies, xml ) Need Help??
rocketperl has asked for the wisdom of the Perl Monks concerning the following question:

what regular expression should i use to separate the numbers only from an alpha numeric expression? My expression looks like this chr1:4775792-4775851 I want to store these two numbers before and after the "-" in two separate variables. should i use \D?

Comment on regular expression
Re: regular expression
by tobyink (Abbot) on Jun 28, 2013 at 11:16 UTC
    #!/usr/bin/env perl use strict; use warnings; use Data::Dumper; my $input = 'chr1:4775792-4775851'; my @numbers = ($input =~ /\b[0-9]+\b/g); print Dumper \@numbers;
    package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name
      what if there is a comma in the file itself

        Which file?

        Nobody said nothin' 'bout no file!

        package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name

        what if there is a comma in the file itself

        what if we did not know what to do with the comma, is it supposed to be part of the number?
        Or even something dividing numbers?

        Cheers, Sören

        (hooked on the Perl Programming language)

        Files are natural habitats for commata and they will not suffer any harm!



Re: regular expression
by hdb (Prior) on Jun 28, 2013 at 11:19 UTC

    If you literally translate two numbers before and after the "-" into a regex, taking into account that a number is a string of digits (\d) and use (...) capture groups, then you have it already.



Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (9)
As of 2014-12-17 20:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (31 votes), past polls