Regular expressions look for similar words--regular expressions

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.