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

An example Analysis of the basic knowledge of regular expression pattern matching string

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces the regular expression pattern matching string basic knowledge of the example analysis, has a certain reference value, interested friends can refer to the next, I hope you read this article after a lot of gains, the following let Xiaobian take you to understand.

introduced

The implementation of a feature in an actual project requires parsing some pattern strings. In the existing code base, in some of the functions that have been implemented, it is used to detect specific characters. The disadvantages of using this method are:

Logically, it's easy to make mistakes.

It's easy to miss checking some boundary conditions

Complex code difficult to understand and maintain

poor performance

See the code base there is a cpp, the entire cpp more than two thousand lines of code, there is a method, light parsing string there are more than 400 lines! One by one, compared to the past, it was really unsightly. Moreover, many of the comments above have expired, and many of the code writing styles are also different. It can basically be judged that they have passed many people's hands.

In this case, there is basically no way to go down this old road, and it is natural to think of using regular expressions. I myself have no practical experience with regular expressions, especially writing matching rules. The first thing he thought of was to find some information from the Internet and get a general understanding of it. However, Du Niang's results were still disappointing. (Of course, if you want to find some more professional knowledge, the results of Du Niang will be heartbreaking every time, all of which are identical copies.) However, usually the life of the mother is still OK) Later, I gave up the query results of the mother, FQ went outside to find, and also found some relatively basic videos (FQ required).

This article can be said to be a summary of the basic knowledge of writing regular expression matching strings. It is mainly divided into the following two parts:

Basic rules for matching strings

Regular Matching, Search and Substitution

The regular expression rule described in this article is ECMAScript. The programming language used is C++. Other aspects will not be introduced.

Basic rules for matching strings

1. Matches fixed strings

regex e("abc");

2. Matches fixed strings, case insensitive

regex e("abc", regex_constants::icase);

3. Match one more character than the fixed string, case-insensitive

regex e("abc. ", regex_constants::icase); // . Any character except newline. 1 character

4. Match 0 or 1 character

regex e("abc? "); // ? Zero or 1 preceding character. Match? the previous character

5. Matches 0 or more characters

regex e("abc*"); // * Zero or more preceding character. Match * previous character

6. Match 1 or more characters

regex e("abc+"); // + One or more preceding character. Match + previous character

7. Matches characters in a specific string

regex e("ab[cd]*"); // [...] Any character inside square brackets. Match any character in []

8. Matches characters of unspecified strings

regex e("ab[^cd]*"); // [...] Any character not inside square brackets. Matches any character other than []

9. Matches a specific string with a specified number

regex e("ab[cd]{3}"); // {n} matches any character before {}, and the number of characters is 3

10. Matches a specific string, specifying a range of quantities

regex e("ab[cd]{3,}"); // {n} matches any character before {}, and the number of characters is 3 or more regex e("ab[cd]{3,5}"); // {n} matches any character before {}, and the number of characters is 3 or more and 5 or less closed intervals

11. Match one of the rules

regex e("abc| de[fg]"); // |matching| Either rule on either side

12. matching packet

regex e("(abc)de+"); // () () denotes a subgroup

13. matched subgroup

regex e("(abc)de+\\1"); // () () indicates a sub-packet, and\1 indicates that the contents of the first packet match here regex e("(abc)c(de+)\\2\\1"); // \2 indicates that the contents of the second packet match here

14. Matches the beginning of a string

regex e("^abc. "); // ^ begin of the string Find substrings that begin with abc

15. Matches the end of a string

regex e("abc.$ ");// $ end of the string Find substrings ending in abc

The above is the most basic matching pattern writing. Usually if you want to match a specific character, you need to use\to escape, such as matching ". ", then\should precede the specific character in the matching string. Beyond the above basic rules, if you don't meet specific needs, you can refer to this link. After understanding the basic matching patterns, you need to use regular expressions to match, find, or replace.

Regular Matching, Search and Substitution

After writing the pattern string, you need to match the string to be matched with the pattern string in a certain rule. There are three ways: match (regex_match), search (regex_search), and replace (regex_replace).

Matching is simple. You pass the string to be matched and the pattern string directly into regex_match and return a bool quantity indicating whether the string to be matched satisfies the rules for pattern strings. Matches the entire str string.

bool match = regex_match(str, e);//matches the entire string str

A lookup is a substring that finds and satisfies the pattern string in the entire string. That is, str returns true as long as there is a string that satisfies the pattern.

bool match = regex_search(str, e);//Find substrings matching e rule in string str

But in many cases, it is not enough to return a bool quantity that matches, we need to get the matching substring. Then you need to group matching strings in pattern strings. Refer to point 12 of Basic Rules for Matching String. Passing smatch into regex_search gives you the string that satisfies each subgroup.

smatch m;bool found = regex_search(str, m, e);for (int n = 0; n < m.size(); ++n) { cout

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

Development

Wechat

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

12
Report