admin管理员组

文章数量:1384579

I understand basic regex expressions, but when I see one like this

/^(28|29|[3-5][7]*[0-9])$/

I can't decipher what it means past a certain point. I get what everything means separately, but together it doesn't make sense to me, I also don't understand what the asterik means in this context. Can anyone explain?

I understand basic regex expressions, but when I see one like this

/^(28|29|[3-5][7]*[0-9])$/

I can't decipher what it means past a certain point. I get what everything means separately, but together it doesn't make sense to me, I also don't understand what the asterik means in this context. Can anyone explain?

Share Improve this question asked Nov 20, 2011 at 15:13 SamSam 2,3479 gold badges39 silver badges53 bronze badges 3
  • I'd remend installing the Regex coach weitz.de/regex-coach - provides syntax highlighting, plain English explanation of regexes, walking through regex matching etc etc – Frank Schmitt Commented Nov 20, 2011 at 15:23
  • 2 Since you've already gotten the answer to your question, I wanted to take this time to drill in an important lesson. ;-) While this was a relatively simple regex (if you've been using them for some time, obviously), your confusion is the exact reason we always remend everybody ment their regular expressions, pletely. It often makes sense to the person that wrote it, but they can be plex and difficult to understand for newbies and experts alike. – senfo Commented Nov 20, 2011 at 15:32
  • @senfo +1 for the "it only makes sense to the person who wrote it" for advanced regexes :D – Esailija Commented Nov 20, 2011 at 15:36
Add a ment  | 

8 Answers 8

Reset to default 7
/^(28|29|[3-5][7]*[0-9])$/

Start by separating the pipes, so it matches:

/^28$/, /^29$/ or /^[3-5][7]*[0-9]$/

The first two are pretty obvious, either 28 or 29 without anything else will be a match, so on the third one:

^[3-5]

Begins with the number 3, 4, or 5

[7]* followed by 0-unlimited amount of sevens until [0-9]$ which means ending in a number inclusively between 0 and 9.

I've broken down the regexp and added lots of whitespace and ments to explain it.

^         # Match beginning of string
( 28      # Start group. Match 28
| 29      # OR 29
| [3-5]   # OR a single digit in the range 3-5
  [7]*    # then the digit 7, any number of times
  [0-9]   # then a single digit in the range 0-9
)         # end group
$         # match end of string

The explanation for * from http://www.regular-expressions.info/repeat.html

The asterisk or star tells the engine to attempt to match the preceding token zero or more times.

then it continues with

The plus tells the engine to attempt to match the preceding token once or more. <[A-Za-z][A-Za-z0-9]*> matches an HTML tag without any attributes. The sharp brackets are literals. The first character class matches a letter. The second character class matches a letter or digit. The star repeats the second character class. Because we used the star, it's OK if the second character class matches nothing. So our regex will match a tag like <B>. When matching <HTML>, the first character class will match H. The star will cause the second character class to be repeated three times, matching T, M and L with each step.

The square brackets (character classes, [...]) are explained on http://www.regular-expressions.info/charclass.html

With a "character class", also called "character set", you can tell the regex engine to match only one out of several characters. Simply place the characters you want to match between square brackets. If you want to match an a or an e, use [ae]. You could use this in gr[ae]y to match either gray or grey. Very useful if you do not know whether the document you are searching through is written in American or British English.

A character class matches only a single character. gr[ae]y will not match graay, graey or any such thing. The order of the characters inside a character class does not matter. The results are identical.

You can use a hyphen inside a character class to specify a range of characters. [0-9] matches a single digit between 0 and 9. You can use more than one range. [0-9a-fA-F] matches a single hexadecimal digit, case insensitively. You can bine ranges and single characters. [0-9a-fxA-FX] matches a hexadecimal digit or the letter X. Again, the order of the characters and the ranges does not matter.

This regex matches:

  28
OR
  29
OR
  3, 4 or 5
  optionally followed by 0 or more 7's
  followed by a number from 0-9

Of course:

/^ means the beginning of the line or string

(28|29|[3-5][7]*[0-9]) is split up into:

  • Select from 3 alternatives: 28, 29 or any character between 3-5, 7 any number of repetitions, 0-9

$/ means end of the line or string

So the * means any number of repetitions.

I can highly remend a program such as Expresso for problems such as this.

Parentheses are creating a group. A pipe separate possible submatches ("OR"). An asterisk is equal to {0,}, and means: Any number of occurrences, as much as possible.

/^(28|29|[3-5][7]*[0-9])$/

^      Start of string
(...)  Either:
              28   OR
              29   OR

               [3-5]  One digit, 3-5 followed by
               [7]*   any occurrence of 7, followed by
               [0-9]  One digit
$      End of string

* (star)

Repeats the previous item zero or more times. Greedy, so as many items as possible will be matched before trying permutations with less matches of the preceding item, up to the point where the preceding item is not matched at all.

*? (lazy star)

Repeats the previous item zero or more times. Lazy, so the engine first attempts to skip the previous item, before trying permutations with ever increasing matches of the preceding item.

Refer this URL for detailed information:-

http://www.regular-expressions.info/quickstart.html

http://www.regular-expressions.info/reference.html

/^(28|29|[3-5][7]*[0-9])$/

Will match numerical input as follows:

  1. 28 or,
  2. 29 or,
  3. 3, 4, or 5 followed by 0 or more 7s followed by a single digit (0-9)

This regex means that are allowed numbers like:

  1. 28
  2. 29
  3. A number that start with a digit from 3 to 5 ([3-5]) have an amount >= 0 of 7 ([7]*) and then a digit from 0 to 9 ([0-9])

本文标签: javascriptTrouble understanding regexStack Overflow