A regular expression is used to search for similar words. A regular expression is used to search for words.

Source: Internet
Author: User

A regular expression is used to search for similar words. A regular expression is used to search for words.

This article demonstrates a variety of Regular Expression structures and techniques used in practical applications to familiarize yourself with the application of regular expressions.

Problem description:

In this instance, you need to solve the following problems.

1. You want to find all the color and color in a string.

2. Find any of the three words ending with "at": bat, cat, or rat.

3. Find the word ending with phobia.

4. Common variants of the Target name "Steven": Steve, Steven, and Stephen.

5. All common forms for matching the term "regular expression.

Solution:

The following lists the regular expressions used to solve these problems in sequence. All these solutions use case-insensitive options.

Question 1:Color and color

\ Bcolou? R \ B

Regular option: case insensitive

Regular genre:. NET, Java, JavaScript, PCRE, Perl, Python, Ruby

Question 2:Bat, cat, or rat

\ B [wb] at \ B

Regular option: case insensitive

Question 3:Word ending with "phobia"

\ B \ w * phobia \ B

Regular option: case insensitive

Question 4:Steve, Steven, and Stephen

\ BSte (? : Ven? | Phen) \ B

Question 5:Variation of "regular expression"

\ Breg (? : Ular·Expressions? | Ex (? : Ps? | E [sn])?) \ B

Regular option: case insensitive

Regular genre:. NET, Java, JavaScript, PCRE, Perl, Python, Ruby

Resolution:

\ B # determine the boundary position of a word

Reg # matching "reg"

(? : # Group but not captured

Ular\# Matching "ular"

Expressions? # Match "expression" or "expressions"

| # Or:

Ex # match "ex"

(? : # Group but not captured

Ps? # Match "p" or "ps"

| # Or:

E [sn] # match "es" or "en"

)? # End a non-capturing group. The entire group is optional.

) # End of a non-capturing Group

\ B # determine the boundary position of a word

Note:The five regular expressions use the word boundary (<\ B>) to ensure that only the entire word is matched.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.