Basic syntax of regular expression
First, let's take a look at two special characters:
'^'
with
'$'
They are used to match the beginning and end of the string, and the separate example
"^ The":
Match
"THE"
String of the beginning
"of despair $":
Match
"of despair"
End string
"^ ABC $":
Match
ABC
Start and
ABC
The end of the string is actually only
ABC
Match match
"Notice":
Matching
Notice
String
You can see if you don't use the two characters we mentioned (the last example), that is,
Mode (regular expression)
You can appear anywhere in the verified string, you didn't lock him on both sides.
There are still a few characters here.
'*', ' ',
with
'?',
They used to represent a number of characters or sequences
.
They said respectively:
"Zero or more", "one or more", and "zero or one."
Here is some examples
: "AB *":
Match string
a
with
0
Or more
B
Composed string
("A", "ab", "abbb", etc.); "AB ":
The same as above, but at least one
b ("ab", "abbb", etc.); "ab?":
match
0
One or one
b; "a? b $":
Matching one or
0
A
a
Plus more than one
B
End string
.
You can also limit the number of characters in large brackets, such as
"AB {2}":
Match one
a
Back followed by two
B
(One can not be less)
("abb"); "AB {2,}":
Minimum two
B ("abb", "abbbb", etc.); "AB {3, 5}": 2
-
5
A
b ("abbb", "abbbb", or "abbbb").
You should also pay attention to you must always specify
(i.e, "{0, 2}", not "{, 2}").
Similarly, you must notice
, '*', ' ',
with
'?'
Same respectively, three ranges are the same,
"{0,}", "{1,}",
with
"{0,1}"
.
Now put a certain amount of characters in the parentheses, such as:
"A (bc) *":
match
a
Followroom
0
One or one
"BC"; "A (BC) {1,5}:
One to one
5
A
"bc."
There is also a character
'│',
Equivalent
Oral
operating
: "Hi│hello":
Matching
"hi"
or
"hello"
of
String
"(B│CD) EF":
Matching
"bef"
or
"CDEF"
String
"" (a │B) * C ":
Matching
-
Multiple (including
0
One)
a
or
b
Back followed by one
c
String
String
;
one point
('.')
Can represent all
Single character
"" a. [0-9] ":
One
a
Follow with a character with a number
(The string containing such a string will be matched, and then omitted in the future)
"^. {3} $":
Take three characters
.
The content hosted in the middle brackets only matches one
Single character
"[ab]":
Match a single
a
or
b (
with
"a│B"
same
"[A-D]":
match
'a'
Until
'd'
Single character
(
with
"A│B│C│D"
and also
"[ABCD]"
Effect
"^ [A-ZA-Z]":
Match strings starting with letters
"[0-9]%":
Matching
Shaped like
x
%
String
", [A-ZA-Z0-9] $":
Match string with a comma in adding a number or alphabet
You can also do you want to get the character column in the middle brackets, you only need to use in the general parentheses.
'^'
Beginning
(i.e., "% [^ a-za-z]%"
Matching
There is a non-letter inside the two percent sign.
String
).
In order to be able to explain, but
^. [$ () │ * ? {/ "
As a character with special sense, you must add these characters
'',
In
PHP3
You should avoid using the most in front of the model
/,
For example
,
Regular expression
"(/ $ │? [0-9] "
Should be called like this
EREG ("(/ $ │? [0-9] ", $ STR) (
do not know
PHP4
Is it the same?
)
Don't forget the characters inside the middle brackets are the exceptions to this routine
-
Inside in the middle bracket
,
All special characters, including
(''),
Will lose their special nature
(i.e., "[* / ? {}.]"
Match strings with these characters
).
and also
,
As
Regx
Manual tells us
"
If the list contains
']',
It is best to take it as the first character in the list.
(
Maybe
'^'
Behind
).
If included
'-',
It is best to put it in the fore or last
, Or
Or a range of second end points
(i.e. [a-d-0-9]
Middle
'-'
Be effective
.
For complete
,
I should involve
Collating Sequences, Character Classes,
Bury
Equivalence classes.
But I don't want to talk about these aspects.
,
These articles in the following articles don't need to be involved.
.
You can
Regex
MAN Pages
Get more news there
.
How to build a pattern to match
Currency quantity
input of
Ok, now we have to do some useful things we have learned: Build a match mode to check if the input information is a representation
Money
Number. We think a representation
Money
There are four ways:
"10000.00"
with
"10,000.00",
Or no fractional part
"10000" and "10,000".
Let us start building this matching mode
: ^ [1-9] [0-9] * $
This is the variable must be
0
Number starting
.
But this also means
Single
"0"
You can't pass the test
.
The following is a solution
: ^ (0│ [1-9] [0-9] *) $ "
only
0
Not
0
The number of the beginning of the beginning
"
, We can also allow a negative number before
: ^ (0│ -? [1-9] [0-9] *) $
This is
"0
or
One
0
There may be a negative number in front of the number
"
All right
,
Ok, now let us don't be so strict, allow
0
beginning
.
Let us give up now
negative
,
Because we don't need to use when you represent coins.
.
We now specify
mode
Used to match the fractional part
: ^ [0-9] (/. [0-9] )? $
This implies that matching strings must start with a Arabian number
.
But attention, in the above mode
"10."
Do not match
,
only
"10"
with
"10.2"
Can
.
(Do you know why)
^ [0-9] (/. [0-9] {2}?
We must have two decimals behind the specified decimal point.
.
If you think this is too harsh
,
You can change
: ^ [0-9] (/. [0-9] {1, 2})?
This will allow the decimal point to have one to two characters.
.
Now let's add a comma used to increase readability (every three digits)
,
We can express this
: ^ [0-9] {1,3} (, [0-9] {3}) * (/. [0-9] {1, 2})?
Don't forget to add
' '
Can be by one
'*'
Alternative If you want to allow blank strings to be entered
(
why
?).
Don't forget the anti-ramp
'/'
in
PHP
Errors may occur in strings
(
Very common mistake
).
Now we can confirm the string
,
We now remove all commas.
Str_Replace (",", ", $ money)
Then look at the type
Double
Then we can do mathematics through him.
.
Construction inspection
Regular expression
Great
,
Let us continue to discuss how to verify one
address
.
In a complete
There are three parts in the address
: POP3
username
(
in
'@'
Everything on the left
), '@',
server name
(
It is the rest of that part
).
Username can contain large-lowercase letters Arabic numbers
,
period
('.'),
Minuscular
('-'), and
Underscore
('_').
The server name is also in line with this rule.
,
Of course, except underline
.
right now
,
The start and end of the username cannot be a period
.
The server is also like this
.
And you can't have two consecutive phones, there is at least one character, so now let's take a look at how to write a matching mode for the user name.
: ^ [_ A-ZA-Z0-9 -] $
It is still not allowed to allow the existence of the period.
.
Let us add it
: ^ [_ a-za-z0-9 -] (/. [_ a-za-za-z0-9 -] ) * $
The meaning of it is
"
Taking less than one specification character (division
.
Accident)
,
Follow
0
Or multiple strings starting with a point
"
A little bit
,
We can use
EREGI ()
replace
EREG (). EREGI ()
Sensitive to case
,
We don't need to specify two scope
"a-z"
with
"A-z" -
Just you need to specify one.
: ^ [_ a-z0-9 -] (/. [_ a-z0-9 -] * $
The next server name is also the same
,
But you have to drop the underline
: ^ [A-Z0-9 -] (/. [A-Z0-9 -] ) * $ DONE.
I only need to use it now
"@"
Connect two parts
: ^ [_ a-z0-9 -] (/. [_ a-z0-9 -] ) * @ [a-z0-9 -] (/. [A-Z0-9 -] ) * $ This is complete
Authentication matching mode
,
Just call
EREGI ('^ [_ a-z0-9 -] (/. [_ a-z0-9 -] ) * @ [A-Z0-9 -] (/. [A-Z0-9 -] ) * $ ', $ EAMIL)
Can you get it?
In
Other usage of regular expressions
Extract strings
EREG () and EREGI ()
One feature is to allow users to extract part of the string through regular expressions.
(
Specific usage you can read the manual
).
For example
,
We want to
Path / URL
Extraction file name
-
The following code is what you need.
: EREG ("(^ ///] *) $", $ Pathorurl, $ regs); Echo $ Regs [1];
Advanced replacement
EREG_REPLACE ()
with
EREGI_REPLACE ()
It is also very useful
:
If we want to replace all the interval negotiations
: EREG_REPLACE ("[/ N / R / T] ", ",", TRIM ($ STR));