Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Need a regular expression to check the first digit

by chandantul (Scribe)
on May 19, 2021 at 03:35 UTC ( #11132725=perlquestion: print w/replies, xml ) Need Help??

chandantul has asked for the wisdom of the Perl Monks concerning the following question:

Hello Smart Monks, I am trying to figure the first digit 5 of 9 digits long string by regular expression. if this 9 digits long string starts with other then 5 then the code will ignore if its starts with 5 then it will write

Please check below i was trying but its not working for me. "503009496" and i was trying below

if (exists $hash2{$celln[$o]} && $celln[$o]=~ /^[5]+$/) { $worksheet->write($r +, 0, $hash2{$celln[$o]}); $worksheet->write($r +11, 1, $celln[$o]); # print "Arrays are different\n"; }

Replies are listed 'Best First'.
Re: Need a regular expression to check the first digit
by GrandFather (Saint) on May 19, 2021 at 04:24 UTC

    Give us some code we can run. See I know what I mean. Why don't you?

    Update: What do you expect /^[5]+$/ to match? What does each component of the expression do? To save us all a little time these are the interesting components:

    • ^
    • [5]+
    • $
    Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond
Re: Need a regular expression to check the first digit
by AnomalousMonk (Bishop) on May 19, 2021 at 06:20 UTC

    Further to GrandFather's remarks:   The regex /^[5]+$/ does not match the string '503009496', but is there any other string it would match? What about the strings

    5 55 555 555555555

    If you find some strings that match, how would they and the strings you know do not match interact with the regex components mentioned by GrandFather? Please see perlre, perlretut, perlrequick and perlreref.


    Give a man a fish:  <%-{-{-{-<

Re: Need a regular expression to check the first digit -- YAPE::Regex::Explain
by Discipulus (Abbot) on May 19, 2021 at 06:53 UTC
    Hello chandantul,

    you already received good hints. A minimal read of the manual is something to do.

    Here the explanation of your regex:

    perl -MYAPE::Regex::Explain -e "print YAPE::Regex::Explain->new(qr/^[5 +]+$/)->explain()" The regular expression: (?-imsx:^[5]+$) matches as follows: NODE EXPLANATION ---------------------------------------------------------------------- (?-imsx: group, but do not capture (case-sensitive) (with ^ and $ matching normally) (with . not matching \n) (matching whitespace and # normally): ---------------------------------------------------------------------- ^ the beginning of the string ---------------------------------------------------------------------- [5]+ any character of: '5' (1 or more times (matching the most amount possible)) ---------------------------------------------------------------------- $ before an optional \n, and the end of the string ---------------------------------------------------------------------- ) end of grouping ----------------------------------------------------------------------

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

      Thanks everyone for your responses, i have resolved the issue with below regex.

      /^[5](?=\d)
Re: Need a regular expression to check the first digit
by davido (Cardinal) on May 19, 2021 at 14:55 UTC

    Let's analyze this regex:

    /^[5]+$/
    • ^ indicates the pattern anchors to the beginning of the string.
    • [...] is a character class. It allows you to create an alternate of choice where a single character in the target string matches any one of the characters within the class.
    • [5] is a character class comprised of only one character, which isn't very useful in this case. You're using a "match any of these" construct but asserting that "these" are any of the following: "5". You could do away with the character class and just put a 5 there.
    • + is a quantifier. It says match one or more of the previous atom. In this case, match [5] one or more times.
    • $ is an anchor to the end of string.

    Taken in full, you're saying starting from the beginning and ending at the end of the target string, match one or more 5 characters, and nothing else. You didn't allow for the trailing digits. Since you anchor to the end of the string, you're asserting that the string can only contain a bunch of 5s.

    If you want to allow for eight trailing digits that are not 5s, you need to specify that too:

    /^5\d{8}$/

    Now you're saying the target has to start with a 5 and then must match eight more digits, at which point the target string must end.


    Dave

Re: Need a regular expression to check the first digit
by tybalt89 (Prior) on May 19, 2021 at 22:50 UTC
    #!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11132725 use warnings; while( <DATA> ) { print /(?<!\d)5\d{8}(?!\d)/ ? ' ' : 'not', " found in : ", $_; } __DATA__ for me. "503009496" and i w for me. "603055496" and i w (6) for me. "703509496" and i w (7) for me. "53009496" and i w (too short) for me. "5093009496" and i w (too long) for me. "503009496" and i w

    Outputs:

    found in : for me. "503009496" and i w not found in : for me. "603055496" and i w (6) not found in : for me. "703509496" and i w (7) not found in : for me. "53009496" and i w (too short) not found in : for me. "5093009496" and i w (too long) found in : for me. "503009496" and i w
Re: Need a regular expression to check the first digit
by Anonymous Monk on May 19, 2021 at 12:55 UTC
    If you're looking for the first instance of the character '5' in a string of 9 numeric characters
    index is your friend
    my $var = '503009496'; my $i = index($var, '5');
    I think that is what you are looking for.
    A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11132725]
Approved by GrandFather
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2021-12-04 11:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    R or B?



    Results (30 votes). Check out past polls.

    Notices?