Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use regular expressions to find similar words

Shulou Source: shulou.com Published: 2022-06-02 03:56:20 10月02日 Update

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.

Tags: Regular word problem size grouping expression article boundary similarity location common scheme more genre solution good practical content several questions variants Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL OPPO Reno Linux Shulou Tech Info vpn