开发者

Can Someone explain this reg ex to me?

开发者 https://www.devze.com 2023-01-17 12:39 出处:网络
I recently asked a question on for开发者_JAVA技巧matting a telephone number and I got lots of responses. Most of the responses were great but one i really wanted to figure out what its doing because i

I recently asked a question on for开发者_JAVA技巧matting a telephone number and I got lots of responses. Most of the responses were great but one i really wanted to figure out what its doing because it worked great. If phone is the following how do the other lines work...what are they doing so i can learn

$phone = "(407)888-9999";

$phone = preg_replace("~[^0-9]~", "", $phone);
preg_match('~([0-9]{3})([0-9]{3})([0-9]{4})~', $phone, $matches);


Let's break the code into two lines.

preg_replace("~[^0-9]~", "", $phone);

First, we're going to replace matches to a regex with an empty string (in other words, delete matches from the string). The regex is [^0-9] (the ~ on each end is a delimiter). [...] in a regex defines a character class, which tells the regex engine to match one character within the class. Dashes are generally special characters inside a character class, and are used to specify a range (ie. 0-9 means all characters between 0 and 9, inclusive).

You can think of a character class like a shorthand for a big OR condition: ie. [0-9] is a shorthand for 1 or 2 or 3 or 4 or 5 or 6 or 7 or 8 or 9. Note that classes don't have to contain ranges, either -- [aeiou] is a character class that matches a or e or i or o or u (or in other words, any vowel).

When the first character in the class is ^, the class is negated, which means that the regex engine should match any character that isn't in the class. So when you put all that together, the first line removes anything that isn't a digit (a character between 0 and 9) from $phone.

preg_match('~([0-9]{3})([0-9]{3})([0-9]{4})~', $phone, $matches);

The second line tries to match $phone against a second expression, and puts the results into an array called $matches, if a match is made. You will note there are three sets of brackets; these define capturing groups -- ie. if there is a match of a pattern as a whole, you will end up with three submatches, which in this case will contain the area code, prefix and suffix of the phone number. In general, anything contained in brackets in a regular expression is capturing (while there are exceptions, they are beyond the scope of this explanation). Groups can be useful for other things too, without wanting the overhead of capturing, so a group can be made non-capturing by prefacing it with ?: (ie. (?:...)).

Each group does a similar thing: [0-9]{3} or [0-9]{4}. As we saw above, [0-9] defines a character class containing the digits between 0 and 9 (as the classes here don't start with ^, these are not negated groups). The {3} or {4} is a repetition operator, which says "match exactly 3 (or 4) of the previous token (or group)". So [0-9]{3} will match exactly three digits in a row, and [0-9]{4} will match exactly four digits in a row. Note that the digits don't have to be all the same (ie. 111), because the character class is evaluate for each repetition (so 123 will match because 1 matches [0-9], then 2 matches [0-9], and then 3 matches [0-9]).


In the preg_replace it looks for anything that is not, ^ inside of the [], 0-9 (basically not a number) and replaces / removes it from that string given the replacement is "".

For the first section, it pulls out the first 3 numbers ([0-9]{3}) the {3} is the number of characters to match the items inside the [] are what to match and since this is inside of paranthesis () it stores it as a match in the array $matches. The second part pulls out the next 3 numbers and the last part pulls out the last 4 numbers from $phone and stores the matches that were matched in $matches.

The ~ are delimeters for the regular expressions.


You know it's a regular expression from the regex tag.

So, you are pattern matching.

The pattern you are matching is: [^0-9] followed by the phone number. [^0-9] is NOT '^' any one digit So, the match after that is any 3 digits, followed by any 3 digits, followed by any 4 digits.

I don't think it will match because of the () around the area code and the dash are missing.

I'd do this:

~\(([0-9]{3})\)([0-9]{3})-([0-9]{4})~'


"[^0-9]" means everything but numbers from 0 to 9. So basically, first line replace everything but numbers with "" (nothing)

[0-9]{3} means number from 0 to 9, 3 times in a row. So it check if you have 3 numbers then 3 numbers than 4 numbers and try to match it with $matches.


Check this tuts

Using Regular Expressions with PHP

  • http://www.webcheatsheet.com/php/regular_expressions.php


$phone = "(407)888-9999";
$phone = preg_replace("~[^0-9]~", "", $phone);
  1. In php you have to delimit regex pattern in some non-alphanumeric character "~" is used here.

  2. [^0-9] is regex pattern used to remove anything out of $phone that is not in 0-9 range remember [^...] will negate the pattern it precedes.

    preg_match('~([0-9]{3})([0-9]{3})([0-9]{4})~', $phone, $matches);

  3. Again in this line of code you have "~" as delimiter and ([0-9]{3}) this part of pattern will return 3 numbers from string (note: {} is used to specify range/number of characters to match) in a different output array dimension (check your $matches variable for result) using ( ) in a pattern results in groups/submatches

0

精彩评论

暂无评论...
验证码 换一张
取 消