2

Can someone elaborate the following regular expression:

/^[a-z]{1}[a-z0-9_]{3,13}$/

and also give some sample strings that satisfy this regular expression?

jbaums
  • 25,349
  • 5
  • 72
  • 114
Subramanyam M
  • 337
  • 2
  • 5
  • 18
  • What is your regex engine? – Myrtle Jun 24 '14 at 11:17
  • @MauriceStam: Why would that matter? (OK, it's generally required to tag the appropriate language, but in this case, it's a pretty universal regex, unless we're targeting a POSIX BR engine). – Tim Pietzcker Jun 24 '14 at 11:20
  • @TimPietzcker Because there are differences. In this case because POSIX does not support greedy. However I agree this is not really common. – Myrtle Jun 24 '14 at 11:22
  • @MauriceStam: POSIX doesn't support *lazy* quantifiers, yes, but the anchors in this regex make that restriction irrelevant as well. – Tim Pietzcker Jun 24 '14 at 13:01
  • @TimPietzcker Lesson learned ;) thanks. – Myrtle Jun 24 '14 at 13:04

6 Answers6

5
  • The ^ anchor asserts that we are at the beginning of the string
  • [a-z]{1} matches one lower-case letter. The {1} is unneeded.
  • [a-z0-9_]{3,13} matches 3 to 13 chars. In case-insensitive mode, in many engines it could be replaced by \w{3,13}
  • The $ anchor asserts that we are at the end of the string

Sample Matches

abcd
a_000
a_blue_tree

See demo.

General Answers to "What Does this Regex Mean?

  1. You can use a tool such as See regex101 to play with a regex. The right pane explains it token by token.
  2. There are several explaining tools based on the same original Perl library, such as this one, on which one of the answers is based.
  3. The ultimate answer can be found in Mastering Regular Expressions, 3rd Ed. and several excellent online tutorials, including the regex FAQ on this site.
Community
  • 1
  • 1
zx81
  • 38,175
  • 8
  • 76
  • 97
4

Explanation: /^[a-z]{1}[a-z0-9_]{3,13}$/

  • ^ - Asserts the start of a string

  • [a-z]{1} Matches exactly one character from a-z.

  • [a-z0-9_]{3,13} Matches any character from a-z or 0-9 but the length range must between 3 to 13.

  • $ End

Example

Avinash Raj
  • 160,498
  • 22
  • 182
  • 229
2

Check Explanation Here

NODE                     EXPLANATION
  ^                        the beginning of the string
  [a-z]{1}                 any character of: 'a' to 'z' (1 times)
  [a-z0-9_]{3,13}          any character of: 'a' to 'z', '0' to '9',
                           '_' (between 3 and 13 times (matching the
                           most amount possible))
  $                        before an optional \n, and the end of the
                           string
Harpreet Singh
  • 2,545
  • 18
  • 30
1

It means: Start(^) with one ({1}) lowercase character([a-z]), then proceed with at least three ({3,) but with a maximum of 13 (13}) characters from the set of lowercase characters, underline and numbers([a-z0-9_]). After that the end of line is expected ($).

a000 satisfies the condition

Thomas Junk
  • 5,296
  • 2
  • 25
  • 39
0

It matches a string starting with a-z followed by 3 to 13 characters from the character set a-z, 0-9 or _.

There are a number of online tools that will explain/elaborate the meaning of a regular expression as well as test them.

Community
  • 1
  • 1
rvalvik
  • 1,499
  • 11
  • 14
0
Assert position at the beginning of the string «^»
Match a single character in the range between “a” and “z” «[a-z]{1}»
   Exactly 1 times «{1}»
Match a single character present in the list below «[a-z0-9_]{3,13}»
   Between 3 and 13 times, as many times as possible, giving back as needed (greedy) «{3,13}»
   A character in the range between “a” and “z” «a-z»
   A character in the range between “0” and “9” «0-9»
   The character “_” «_»
Assert position at the end of the string (or before the line break at the end of the string, if any) «$»

Generated using RegexBuddy

Myrtle
  • 5,569
  • 30
  • 46
  • @ThomasJunk Whether the slashes are delimiters depends on the regex engine or the language in which it has been written which is not known in the question. – Myrtle Jun 24 '14 at 11:19
  • @jbaums I agree, therefore I have removed them. However I do not know of any language using that delimited syntax so the actual question should not have 'included' them ;). – Myrtle Jun 24 '14 at 11:24
  • So sorry Maurice... That is idiotic, don't know what happened. So... Subbu, did Maurice's answer your question? Please give us some feedback. :) – zx81 Jun 25 '14 at 08:46