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

Example analysis using regular expressions

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article shares with you the content of sample analysis using regular expressions. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Brief introduction of regular expression

Regular expression, also known as regular representation, conventional representation. (English: Regular Expression, often abbreviated as regex, regexp or RE in code), a concept in computer science. Regular expressions use a single string to describe and match a series that conforms to a syntactic rule. In many text editors, regular expressions are often used to retrieve and replace text that matches a certain pattern.

Example 1: matches a number of 5-12 digits: ^\ d {5pr 12} $

First, we introduce two special symbols, ^ for the beginning of the string and $for the end of the string, which are used to limit the scope of the match. Then\ d is used to match a number, followed by a {}, in which the number is entered. When entering a number, such as {3}, it means that the number is matched three times; when entering two numbers, as in the example, it can be matched 5-12 times.

Example 2: match words that begin with the letter a:\ ba\ w *\ b

You can use characters to match directly in regular expressions, such as a to match a pattern with the letter a. \ b represents the edge of the character. \ w means to match all characters except spaces. Some symbols can be used in regular expressions to indicate how many times to match, where * means to match 0 times, + means to match 1 times,? Indicates a match of 0 or 1 times. So in the above example,\ w* means a followed by 0 or more characters. So the whole pattern matches characters that start with a.

Example 3: matching landline phone number:\ (? 0\ d {2} [) -]?\ d [8]

This regular expression matches a phone number such as 88886666 or 22334455. First of all, we should pay attention to it, because (? Symbols such as others have a special meaning in regular expressions, so you need to change your mind when you want to use real such symbols. The first\) in this example? It means (0 or 1 occurrence). Next is the area code, because the area code is a 3-digit number and the starting number must be 0, so using 0\ d {2} can perfectly match this requirement. Then there is the use of []. Multiple values can be placed in [], indicating that the next bit matches one of these characters. Like [) -] here? It means to match one of the left parentheses, spaces, or -, 0 or 1 times. Finally, use\ d {8} to match 8 digits to meet the requirements.

Example 4: matches the first part of the word ending in ing:\ b\ w + (? = ing\ b)

Zero-width assertions are used here, and the (? = exp) in the zero-width assertion matches the front part of the subsequent requirement. The (? = ing\ b) in this example matches the characters that end in ing.

Thank you for reading! This is the end of this article on "sample Analysis of using regular expressions". I hope the above content can be helpful to you, so that you can learn more knowledge. If you think the article is good, you can 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