This paper is familiar with the application of regular expressions by showing the structure of different regular expressions and the techniques used in practical applications.
Problem Description:
In this example you have to solve the following problems.
1, want to find all the color and colour in a string.
2. To find any of these 3 words that end with "at": Bat, Cat, or rat.
3, want to find the end of phobia words.
4. The usual variants of the name "Steven" you want to find: Steve, Steven and Stephen.
5, want to match the term "regular expression" all the common forms.
Solution:
The following is a sequential list of regular expressions used to resolve these issues. In all of these solutions, the case-insensitive option is used.
question 1:color and colour
\bcolou?r\b
Regular options: case-insensitive
Regular genres:. NET, Java, JavaScript, PCRE, Perl, Python, Ruby
Question 2:bat, cat or rat
\b[bcr]at\b
Regular options: case-insensitive
Question 3: words ending with "phobia"
\b\w*phobia\b
Regular options: case-insensitive
Question 4: Steve, Steven and Stephen
\bste (?: ven?| Phen) \b
Question 5:variants of "regular expression"
\breg (?: Ular· expressions?| Ex (?:p s?| E[SN])? \b
Regular options: case-insensitive
Regular genres:. NET, Java, JavaScript, PCRE, Perl, Python, Ruby
Analytical:
\b #判断一个单词边界位置
Reg #匹配 "Reg"
(?: #分组但是不捕获
Ular\ # match "Ular"
Expressions? # match ' expression ' or ' expressions '
| Or
Ex # match "ex"
(?: # grouped but not captured
Ps? # match ' P ' or ' ps '
| Or
E[SN] # match "es" or "en"
)? # end non-capture grouping, entire grouping is optional
) # End of non-capture grouping
\b #判断一个单词边界位置
Note: all 5 regular expressions use a word boundary (<\b>) to ensure that only the whole word is matched.
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.