This is the first day of my participation in Gwen Challenge
Main functions and ground rules
There are a lot of information on the Internet, the good and bad are mixed, and the official documents are very hard to write. I have chewed on them for a long time and summarized some dry things. There will be several articles in this series, based on the general re and PHP syntax to do
Regular is a little obscure, right?
Regular expression literally means “regular expression” (RE for short in English and RE for short in Chinese). In fact, everyone has used regular, which is our search function. You input the keyword you want to match, such as AV. This is regulation.
The first lesson is over, class dismissed!
What’s the use of the re anyway?
Now that we know what regex does, let’s look at what else regex can do:
- Fast and simple data verification. For example, your password must contain 8 digits or English letters, verify the input ID number is in accordance with the standard and so on. If there are no regex, do we loop through them bit by bit? !
- Data search and operation. For example, you can use regular expressions to find a given title or text, and then you can extract, replace, delete, modify, and so on.
- Apply the rule to generate a string that matches the rule. This is the reverse application of regulars, such as captchas and random passwords, which we often encounter. What you think is random, there are rules.
OK, now that we know the main function of re, let’s talk about the next step
Some basic rules for using regex
-
Features are matched from left to right. The first is that the regular expression reads and matches from left to right. The second is that the target string reads and matches from left to right. If there is a match, the value of the match is returned.
-
Again, order. In branching, make sure you write the more complicated rules first. Because of the left-to-right matching feature, when you write a re, if you allow two or more matching features, and the two features are very similar, you must write more complex rules first, otherwise you will not be able to match. Here’s an example:
$a='55555-4444'; // The U.S. zip code can be 5 digits, or 5 digits plus 4 optional digits
- \ d \ d {5} {4} | \ d {5} / / matches [0] = > 53233-3233
\ d {5} | \ d {5} - \ d {4} / / in turn, if the order is only matches [0] = > 53233, leads to can't return 5 digital + 4 optional number
-
Greed Greed: Greed here has a broad and narrow sense of two meanings. By default, the regular expression will match the most characters according to the characteristics you define. For example, \d+ means that any number is repeated 1 or n times. The target string is 335522555, so it will return the entire string instead of 3. To cancel the greed mode, \d*? I’m just going to return 3, which is a demonstration of greed in the narrow sense. But sometimes even if you cancel greed, the system will still make the maximum match based on the characteristics that you specify, and that’s greed in a broad sense, and that can’t be cancelled, and we’ll talk about that in the next video.
-
Regular expressions are case sensitive by default. If you want to be case insensitive, you can use the/I or (? I) the way to cancel, more specifically in the next section.
-
Regular expressions recognize Spaces by default, so if a space is entered incorrectly and a match fails, you can avoid this problem by using the /x modifier, as discussed in the next section.
-
Expressions must start and end with delimiters. Delimiters can be customized, and delimiters are considered special if they appear in expressions. For character matching, delimiters must be escaped by a backslash \.
-
When matching a string, the default target string is a single line. You need to use the feature modifier /m to match a multi-line string. Otherwise, the match is incomplete, as described in the next section.
Ok, that’s the end of the second lesson! Get an impression of these rules and experience them in practice.