Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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


In reply to Re: Need a regular expression to check the first digit by davido
in thread Need a regular expression to check the first digit by chandantul

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2022-06-29 02:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My most frequent journeys are powered by:









    Results (94 votes). Check out past polls.

    Notices?