IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Langage Perl Discussion :

Regular expression perl


Sujet :

Langage Perl

  1. #1
    Candidat au Club
    Homme Profil pro
    peu importe
    Inscrit en
    Septembre 2015
    Messages
    2
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Suisse

    Informations professionnelles :
    Activité : peu importe
    Secteur : Conseil

    Informations forums :
    Inscription : Septembre 2015
    Messages : 2
    Points : 2
    Points
    2
    Par défaut Regular expression perl
    Hi all,

    I have a issue with a regex including two numbers :

    Explanation:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
    my $range_schedule_month	= [0-1][0-2] ;
     
    chomp ($schedule_month = <STDIN>);
    last if ( $schedule_month =~ m/$range_schedule_month/) ;
    Idea is that user have tu put STDIN number beetween "00 and 12" but no "00".

    I have tried various things without success.

    Does anyone have the solution ?

    (I also speak french and spanish)

  2. #2
    Membre émérite
    Avatar de Jasmine80
    Femme Profil pro
    Bioinformaticienne
    Inscrit en
    Octobre 2006
    Messages
    3 157
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Âge : 44
    Localisation : Royaume-Uni

    Informations professionnelles :
    Activité : Bioinformaticienne
    Secteur : Santé

    Informations forums :
    Inscription : Octobre 2006
    Messages : 3 157
    Points : 2 673
    Points
    2 673
    Par défaut
    Hello Patit,

    Maybe something like this :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
    # verification of 2 digits format
    last if ( $schedule_month !~ m/^\d\d$/) ;
    # verification if it is inside the range
    last if (( $schedule_month < 1 ) || ( $schedule_month > 12 )) ;

    Wait to see advices from other people ... something else could be better
    -- Jasmine --

  3. #3
    Membre émérite
    Avatar de Jasmine80
    Femme Profil pro
    Bioinformaticienne
    Inscrit en
    Octobre 2006
    Messages
    3 157
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Âge : 44
    Localisation : Royaume-Uni

    Informations professionnelles :
    Activité : Bioinformaticienne
    Secteur : Santé

    Informations forums :
    Inscription : Octobre 2006
    Messages : 3 157
    Points : 2 673
    Points
    2 673
    Par défaut
    You can also merge them :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    last if ( ($schedule_month !~ m/^\d\d$/) || ($schedule_month < 1) || ($schedule_month > 12) ) ;
    -- Jasmine --

  4. #4
    Membre éclairé
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Janvier 2013
    Messages
    388
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : Conseil

    Informations forums :
    Inscription : Janvier 2013
    Messages : 388
    Points : 692
    Points
    692
    Par défaut
    Hi,
    Try this regex :
    0 and a number between 1 and 9, or 1 and a number between 0 and 2.

  5. #5
    Candidat au Club
    Homme Profil pro
    peu importe
    Inscrit en
    Septembre 2015
    Messages
    2
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Suisse

    Informations professionnelles :
    Activité : peu importe
    Secteur : Conseil

    Informations forums :
    Inscription : Septembre 2015
    Messages : 2
    Points : 2
    Points
    2
    Par défaut
    Thanks a lot for all answers !

    __dardanos__'s solution works perfectly and seems better for me. Regex is directly applied into STDIN.

    Thanks again, you are so nice.

    Pat

  6. #6
    Expert éminent Avatar de CosmoKnacki
    Homme Profil pro
    Justicier interdimensionnel
    Inscrit en
    Mars 2009
    Messages
    2 858
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Charente Maritime (Poitou Charente)

    Informations professionnelles :
    Activité : Justicier interdimensionnel

    Informations forums :
    Inscription : Mars 2009
    Messages : 2 858
    Points : 6 556
    Points
    6 556
    Par défaut
    Tu peux aussi définir un rang de cette manière '01'..'12':
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    while (1) {
        chomp (my $schedule_month=<STDIN>);
        last if ( grep { $schedule_month eq $_ } '01'..'12');
    }
    ou avec List::MoreUtils:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    use List::MoreUtils 'any';
    while (1) {
        chomp (my $schedule_month=<STDIN>);
        last if any { $_ eq $schedule_month } '01'..'12';
    }
    Brachygobius xanthozonus
    Ctenobrycon Gymnocorymbus

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. perl regular expression
    Par yoruichiy dans le forum Langage
    Réponses: 5
    Dernier message: 04/10/2013, 19h52
  2. [Regular expression] Possessive quantifiers
    Par Pragmateek dans le forum Langage
    Réponses: 1
    Dernier message: 24/06/2006, 12h55
  3. [debutant] Regular Expression
    Par absolut75 dans le forum Collection et Stream
    Réponses: 5
    Dernier message: 13/06/2006, 01h43
  4. [RegEx] Empty regular expression
    Par Khrysby dans le forum Langage
    Réponses: 2
    Dernier message: 24/04/2006, 22h25
  5. [Regular Expressions] Numérotation romaine
    Par ramalho dans le forum Valider
    Réponses: 3
    Dernier message: 24/02/2004, 17h43

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo