Objective
Before learning other knowledge, often see regular expression, a start to disagree, later seen more, think this thing certainly quite diao, it is necessary to learn a learning, so from the Internet to find some information to start learning, Google search, the tutorial is quite many, but do not know which easy to learn some, A brief look at the time, basic grasp, but learn not system, one day to Zhang Ziyang a blog, feel write very good, went to see his homepage, found Zhi brother also wrote regular expression tutorial, with the reverence for Zhi elder brother, look very exciting, an afternoon to see, feel good.
What is a regular expression
It is used to find text exactly , or to return a search result, or to replace the content; In turn, it can format the input information.
Regular Expressions Learn what
Regular expressions can be used as a tool to learn, basic, common use can, not commonly learned will also forget, in this purpose, I will be different from the use of the regular expression of knowledge classification.
Simple match matches a single character
Focus on learning ".", "?", "[]", "\s", "\d", "\w" and "\"
- “.” : Matches any single character
- “?” : matches 0 or 1 specific characters (its previous character)
- "[]": matches one character in several characters
- "\s": matches a null character, including a space, Tab, carriage return, line feed, equivalent to [\f\n\r\t\v]
- "\d": matches a single number equivalent to [0-9]
- "\w": matches a single uppercase and lowercase letters, numbers, underscores, equivalent to [a-za-z0-9_]
- "\": an escape character that matches a character with special meaning, such as "." or "?"
In addition, \s\d\w's antisense match is \s\d\w, which is the matching of non-null characters, non-numeric characters, and non-uppercase and lowercase letters, numbers, underscores, characters
Learn these, the basic search function can be done.
Example follow-up supplement
Intermediate match matches multiple characters
Focus on learning "+", "*", "^", "$", "{}", "\b"
- "+": matches 1 or more specific characters (its previous character)
- "*": matches 0 or more specific characters (its previous character)
- "^": matches the first text, used in "[]" to represent the matching antonyms, such as [^0-9] matching non-numeric
- "$": Match text end
- ' {} ': matches a specified number of specific characters (its previous character)
- "\b": Match character boundaries (dividing the boundaries of words, except for underscores)
- "\b": In contrast to "\b", matches the "non-word" boundary (i.e., letter or kanji)
Example follow-up supplement
Advanced matching with sub-mode and lazy mode matching
Focus on Learning "()", "|", "greedy matching and lazy matching"
- "()": matches the string in parentheses as a whole, as a character, such as (good), when the text contains "good", "God" and "Ood" do not match to
- | : "or" match, match any of the two regular expressions, such as "Good|bad", match contains good or bad, can also be used in "()"
- Greedy match: The default matching pattern . It will match as many characters as possible. It first looks at the entire string, if it does not match, shrinks the string, encounters a possible match of text, stops shrinking, expands the text, and when it finds a match, it is not anxious to save the match to the matching set, but continues to extend the text until it cannot continue to match or extend the entire string. The last matching text (also the longest) is then saved to the matching collection. So, it's greedy.
- Lazy matching: It matches as few characters as possible, it starts with the first character, and once the condition is met, it is immediately saved to the matching collection and then continues the lookup. That's why it's lazy.
The following are the differences between greedy and lazy matching characters:
Example follow-up supplement
Postscript
This article is my study of this tutorial summary and thinking of finishing, summed up also not very good, hope to see it people can learn from, what questions welcome discussion ~
Regular Expression Learning notes