Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

regular expression

by rocketperl (Sexton)
on Jun 28, 2013 at 11:11 UTC ( [id://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 (Canon) 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 (Monsignor) 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
Domain Nodelet?
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?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (2)
As of 2024-04-25 07:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found