Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
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
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?
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 having an uproarious good time at the Monastery: (3)
As of 2015-08-31 08:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The oldest computer book still on my shelves (or on my digital media) is ...













    Results (354 votes), past polls