Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

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?

Replies are listed 'Best First'.
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

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

        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)

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?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1041219]
Approved by hdb
shmem puts down the Canon garments and dresses as Chancellor
[vrk]: Congratulations. You should have Cookies
[shmem]: thanks vrk - but I'll rather have a plate of "silly con charme"
Lady_Aleena is trying to explain perl regexen to people right now and explaining how variables expand in regexen.
[Lady_Aleena]: The people I'm talking with think /$var/ looks weird with some saying it shouldn't work.

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2017-04-28 09:41 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (520 votes). Check out past polls.