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

how to do get digit numbers like -,.?

by virudinesh (Acolyte)
on Jun 01, 2013 at 06:26 UTC ( #1036384=perlquestion: print w/replies, xml ) Need Help??
virudinesh has asked for the wisdom of the Perl Monks concerning the following question:

use warnings; use strict; my @f =( "Figure. 89-9","Figure 12-8","Figure 1.2"); foreach(@f) { if($_=~m/fig(. |[a-z])*(\d+)(\-(\d+))?/i) { print " $2-$4 \n"; } }

its output comes like

89-9 12-8 1-

but am need output like this one 89-9 12-8 1.2

how to print both one's - (or). how to handle both in single regular expression

Replies are listed 'Best First'.
Re: how to do get digit numbers like -,.?
by hdb (Prior) on Jun 01, 2013 at 06:49 UTC

    ++ for asking a complete question. Here is a complete answer:

    use warnings; use strict; my @f =( "Figure. 89-9","Figure 12-8","Figure 1.2"); foreach (@f) { print " $1\n" if /(\d+[-.,]\d+)/; }
      Just expanding upon your good answer to include the abbreviation 'Fig':
      perl -E 'my @f =("2013-06-01", "fig 1-1", "Fig. 2.2", "Figure. 89-9"," +Figure 12-8","Figure 1.2"); foreach (@f) { print " $1\n" if /fig(?:ure)?\.?\s(\d+[-.,]\d+)/i; }' 1-1 2.2 89-9 12-8 1.2
Re: how to do get digit numbers like -,.?
by Laurent_R (Abbot) on Jun 01, 2013 at 09:51 UTC

    hdb gave you a solution, but just to make sure that you understand your error, your problem simply comes from the fact that your regex does not include the dot.

Re: how to do get digit numbers like -,.?
by Anonymous Monk on Jun 01, 2013 at 06:39 UTC
    Use a "character class" or an alternation, perlintro shows examples of both

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1036384]
Approved by shmem
help
Chatterbox?
[tye]: -Mouse
[Corion]: Option a) would mean launching cmd.exe /k c:\path\to\ batchfile- launching-perl- script.cmd. Option b) would be to add pause as the last line of said batch file.
[LanX]: First day after holidays ... and already stressed by the fact that colleagues changed stuff without communication ... apparently I'm the only one trying to fight entropy
[Corion]: LanX: The command is always in the history if you typed it in before. If you didn't type the command into the command line, it will not be there. I think there is doskey which can stuff command lines into the history
LanX damns the cult of CB ;-)
LanX WTF WTF WTF
[LanX]: please forget my last 3 posts
[LanX]: Yeah option a doesn't go into history
[LanX]: probably I need to teach the app to restart after C-c Kill
[Corion]: LanX: Maybe have an infinite-loop cmd file? Much easier than trying to manage that from within Perl IMO

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (9)
As of 2017-03-27 15:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should Pluto Get Its Planethood Back?



    Results (320 votes). Check out past polls.