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

What are the basics of regular expression pattern matching string

2025-01-28 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 what the basic knowledge of regular expression pattern matching string is. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

Basic rules for matching strings

1. Match a fixed string

Regex e ("abc")

two。 Matches fixed strings and is case-insensitive

Regex e ("abc", regex_constants::icase)

3. Match one more character in addition to a 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? Previous character

5. Match 0 or more characters

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

6. Match one or more characters

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

7. Match characters in a specific string

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

8. Matches characters that are not specific strings

Regex e ("ab [^ cd] *"); / / [...] Any character not inside square brackets. Match any character not within []

9. Matches a specific string and specifies the number

Regex e ("AB [CD] {3}"); / / {n} matches any character before {} and the number of characters is 3

10. Match a specific string and specify a range of quantities

Regex e ("ABCD] {3,}"); / / {n} matches any character before {} and the number of characters is 3 or more regex e ("AB[ CD] {3pr 5}"); / / {n} matches any character before {} with more than 3 characters and less than 5 closed intervals

11. Match a rule in a rule

Regex e ("abc | de [FG]"); / / | match | any rule on both sides

twelve。 Matching grouping

Regex e ("(abc) de+"); / / () () represents a subgroup

13. Match subgroup

Regex e ("(abc) de+\\ 1"); / / () () represents a subgroup, while\ 1 indicates that the content of the first packet is matched at this location regex e ("(abc) c (de+)\\ 2\ 1"); / /\ 2 indicates that the content of the second packet is matched here

14. Match the beginning of a string

Regex e ("^ abc."); / / ^ begin of the string looks for substrings that begin with abc

15. Match the end of a string

Regex e ("abc.$"); / / $end of the string looks for 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 escape with\, for example, if you need to match "." in the matching string, you should add\ before the specific character in the matching string. Given the above basic rules, if you do not meet your specific needs, you can refer to this link. After using to understand the basic matching patterns, you need to use regular expressions to match, find, or replace.

Regular matching, finding and substitution

After writing the pattern string, you need to match the string to be matched with the pattern string. There are three ways: match (regex_match), find (regex_search), replace (regex_replace).

Matching is simple, passing the string to be matched and the pattern string directly into the regex_match and returning a bool quantity indicating whether the string to be matched meets the rule of the pattern string. Matches the entire str string.

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

Finding is a substring that finds and satisfies the pattern string throughout the string. That is, true is returned as long as there is a satisfying pattern string in str.

Bool match = regex_search (str, e); / / find substrings in the string str that match the e rule

But in many cases, it is not enough to return a matching amount of Bool. we need to get the matching substring. Then you need to group matching strings in the pattern string, refer to point 12 of [basic rules for matching strings]. Pass the smatch into the regex_search, and you can get a string that satisfies each subgroup.

Smatch cout 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

Internet Technology

Wechat

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

12
Report