Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use regular expressions to find similar words

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how to use regular expressions to find similar words. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

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. Want to find any of the three words that end with "at": bat, cat, or rat.

3. Want to find the word that ends in phobia.

4. Common variants of the name "Steven" you are looking for: Steve, Steven, and Stephen.

5. Want to match all the common forms of the term "regular expression".

Solution:

The regular expressions used to solve these problems are listed in order below. Case-insensitive options are used in all of these solutions.

Question 1:color and colour

\ bcolou?r\ b

Regular option: case-insensitive

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

Question 2:bat, cat, or rat

\ b [BCR] at\ b

Regular option: case-insensitive

Question 3: words ending in "phobia"

\ b\ w*phobia\ b

Regular option: case-insensitive

Questions 4:Steve, Steven and Stephen

\ bSte (?: ven? | phen)\ b

Question 5: variants of "regular expression"

\ breg (?: ular ·expressions? | ex (?: ps? | e [sn])?)\ b

Regular option: case-insensitive

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

Parsing:

\ b # determine the boundary position of a word

Reg # matches "reg"

(?: # grouped but not captured

Ular\ # matches "ular"

Expressions? # matches "expression" or "expressions"

| # or:

Ex # matches "ex"

(?: # grouped but not captured

Ps? # matches "p" or "ps"

| # or:

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

)? # end the non-capture packet, and the whole packet is optional

) # end of non-capture packet

\ b # determine the boundary position of a word

Note: all five regular expressions use word boundaries () to ensure that only the entire word is matched.

This is the end of the article on "how to use regular expressions to find similar words". I hope the above content can be helpful to you, so that you can learn more knowledge. If you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report