The regular expression has N problems. well, as described in the previous post, I have learned regular expressions for three or two days, I am not very familiar with this book. I am focusing on Baidu Encyclopedia. Question 1: I want to ask, regular expressions generally require N regular expressions.
Well, as described in the previous post, I have already started to learn regular expressions.
I have been exploring for three or two days, and I am confused about some problems.
I am not very detailed about this book. I learned it from Baidu Encyclopedia.
Speech Conversion
============================================
============================================
Question 1:
I would like to ask, regular expressions usually need to be used
/Wrap the content? /
If there is a modifier, will the modifier be written after the second/number? (For example, if/aAa/I uses I as a modifier, only 2nd/numbers are placed after it)
I learned it from Baidu Baike. It says:
$
Match the row Terminator. For example, the regular expression weasel $ can match the end of the string "He's a weasel"
But cannot match the string "They are a bunch of weasels ."
So I used PHP to write code debugging,
$ Str2 = "He's a weasel ";
$ Pm2 = 'weasel $ ';
If (preg_match ($ pm2, $ str2 ))
{
Echo 'True
';
}
Else
{
Echo 'false
';
}
But the result is a failure.
After a long time, I finally
$ Pm2 = 'weasel $ ';
Change
$ Pm2 = '/weasel $ /';
Succeeded, but Encyclopedia did not say so ..
Maybe I did not take it seriously.
Check that the search content is enclosed by a slash (/) on the left and right, and the modifier is behind the slash (/) on the right? I don't even know the writing rules.
============================================
============================================
Question 2:
In the first table mentioned "metacharacters" in Encyclopedia, I understood the role of the number * mentioned in the fourth row. I used it to match it with. *, but later it said, "for example .* Yes No matter what "
So I used the above code for testing, but it showed that I could not find it.
~~~~~~~~~~~~~~~~~~~~~~
$ Str1 = <
Test;
$ Pm1 = '/ .* /';
If (preg_match ($ pm1, $ str1 ))
{
Echo '1. found
';
}
Else
{
Echo '1. not found
';
}
~~~~~~~~~~~~~~~~~~~~~~
Here, what it says .* Contains/. I realized that/may be mistaken for an expression Terminator, so I changed it to \/. However, it does not work. In addition, I understood its description: Content Can be .* This expression is matched, but I don't know how to write it.
============================================
============================================
Question 3:
In fact, I understand \ as an escape character, right? I feel that the statement in the encyclopedia is not very clear, just like in C language or PHP, \ n is escaped as a carriage return,
\ T is a Tab, \ is \, \/is/, \ $ is $ or something
============================================
============================================
Question 4:
The following code is based on the regular expression \ Can match "the" 'in the string "for the wise "'
$ Str1 = < For the wise
Test;
$ Pm1 = '/\ /';
If (preg_match ($ pm1, $ str1 ))
{
Echo '1. found
';
}
Else
{
Echo '1. not found
';
}
The reason cannot be found is that he added the following sentence "this metacharacter is not supported by all software"
============================================
============================================
Question 5:
About? No. introduction, Baidu does not fully understand, and found other documents
I understand it as app? Path? The three letters in the app before "#" appear before "path" only once, but I cannot understand how to set the character.
$ Str1 = < Abcdefg
Test;
$ Pm1 = '/df? E /';
If (preg_match ($ pm1, $ str1 ))
{
Echo '1. found
';
}
Else
{
Echo '1. not found
';
}
But it can also be matched! Where is df? even f is behind e. I feel like I have learned it in vain. it seems that I have not understood the regular expression matching rules!
------ Solution --------------------
Half done. That's how you understand
/Tell the regularizedcondition to start the second one/tell the regularizedcondition to end. In fact, this is just a combination. you can even use # or other characters to replace this so-called start and end.
In this case, if/xxxxa/xxxxb/, the regular expression will end when xxxxa is reached, and an error will occur later, at this time, we need to escape it to understand that it is only a character in the middle, not the end. This is why escape is used.
$ Str1 = <
Test;
$ Pm1 = '/ .* /';
The matching condition here is as follows: Starting from any character End your str1 It certainly does not match.
Let others tell you the rest. haha
------ Solution --------------------
Question 1:
/Here is the real regular expression/
Regular expressions must be enclosed in the middle. if there is a modifier, add it to the end of the last /.
For example, preg_match ('/aaa $/is'). here the is a modifier.
Of course, please note that if the regular character contained in // also contains/, you need to escape it.
Note that the regular expression is included in // or # @. you can try it.
Question 2:
Of course you cannot find it here. what you want to find is And And the intermediate content, the content you give is only" ", It must not meet the requirements. if you cannot find it, it is normal. if you find it, there is a problem with your rp.
Question 3:
As I said in Question 1, you use/to wrap the regular expression. If your regular expression also contains/, you must escape it. Also, $ ^? These are all special characters. if you want to directly match those special characters, you must also escape them. Otherwise, the computer will process them as special characters.
Example:
Content: aaabbb $ ccc
If you want to match bb $ cc
1:/bb $ cc/This is of course a problem. it is literally consistent with the above, but the computer will regard $ as the final meaning.
2:/bb \ $ cc/this is correct