C++ 网站建设定制开发正则表达式教程解释了 C++ 网站建设定制开发中正则表达式的工作,包括、搜索、替换、网站建设定制开发输入验证和标记化的功能。
网站建设定制开发几乎所有的编程语言都网站建设定制开发支持正则表达式。 C++ 从 C++11 网站建设定制开发开始直接支持正则表达式。网站建设定制开发除了编程语言之外,网站建设定制开发大多数文本处理程序(如、网站建设定制开发高级文本编辑器等)网站建设定制开发都使用正则表达式。
在本教程中,我们将探讨正则表达式的一般细节以及 C++ 编程方面的细节。
中的基础符号
^开头()组[]或,{}几次$结尾
- 1
- 2
- 3
- 4
- 5
1. C++ 中的正则表达式 (Regex)
正则表达式是包含一系列字符的表达式,这些字符定义了可用于字符串搜索算法、查找或查找/替换算法等的特定搜索模式。正则表达式也用于输入验证。
大多数编程语言要么为正则表达式提供内置功能,要么通过库提供。从 C++11 开始,C++ 通过 标准库提供正则表达式支持。
用于解析正则表达式的正则表达式处理器将其转换为内部表示,该表示被执行并与表示正在搜索的文本的字符串匹配。 C++11 使用 ECMAScript 语法作为正则表达式的默认语法。 ECMAScript 很简单,但它提供了强大的正则表达式功能。 让我们看看我们在正则表达式中指定的一些模式,如范围规范、重复模式等。
1.1 范围规范
指定字符或文字的范围是正则表达式中使用的最简单的标准之一。
例如,我们可以指定一个从a到z的小写字母范围,如下所示:[a-z],这将只匹配一个小写字符。
下列条件:[A-Za-z0-9]
上面的表达式指定了包含一个大写字母、一个小写字母和0到9之间的数字的范围。
上述表达式中的方括号([])具有特殊含义,即用于指定范围。如果你想包含一个括号作为表达式的一部分,那么你需要转义它。
那么下面的表达式,[\[0-9]
上面的表达式表示一个左括号和一个0到9范围内的数字作为正则表达式。
但请注意,当我们使用 C++ 编程时,我们需要使用 C++ 特定的转义序列,如下所示:[\\[0-9]
1.2 重复模式
我们上面指定的范围示例只匹配一个字符或文字。如果我们想匹配多个字符,我们通常在模式中指定“表达式修饰符”,从而使其成为一个重复的模式。
表达式修饰符可以是+
,表示匹配一个模式出现一次或多次,也可以是*
,表示匹配一个模式出现零次或多次。
例如,下面的表达式,
[a-z]+
匹配 a、aaa、abcd、softwaretestinghelp
等字符串。请注意,它永远不会匹配空白字符串。
[a-z]*
将匹配一个空白字符串或任何上面的字符串。
如果要指定一组字符匹配一次或多次,则可以使用括号,如下所示:(Xyz)+
上面的表达式将匹配Xyz、XyzXyz
和XyzXyz
等等。
2. C++正则表达式的例子
考虑一个匹配 MS-DOS 文件名的正则表达式,如下所示。
char regex_filename[] = “[a-zA-Z_] [a-zA-Z_0-9]*\\.[a-zA-Z0-9]+”;
- 1
上面的正则表达式可以解释如下:
匹配一个字母(小写,然后大写)或下划线。然后匹配零个或多个字符,其中每个字符可以是字母、下划线或数字。然后匹配文字点(.)。在点之后,匹配一个或多个字符,其中每个字符可以是表示文件扩展名的字母或数字。
3. C++正则表达式中使用的函数模板
现在让我们讨论在 C++ 中编写正则表达式时的一些重要函数模板。
3.1 regex_match()
此函数模板用于匹配给定的模式。如果给定的表达式与字符串匹配,则此函数返回 true
。否则,该函数返回 false
。
以下是演示 regex_match
函数的 C++ 编程示例。
#include <iostream>#include <string>#include <regex>using namespace std; int main () { if (regex_match ("softwareTesting", regex("(soft)(.*)") )) cout << "string:literal => matched\"; const char mystr[] = "SoftwareTestingHelp"; string str ("software"); regex str_expr ("(soft)(.*)"); if (regex_match (str,str_expr)) cout << "string:object => matched\"; if ( regex_match ( str.begin(), str.end(), str_expr ) ) cout << "string:range(begin-end)=> matched\"; cmatch cm; //匹配的词语检索出来 regex_match (mystr,cm,str_expr); smatch sm; regex_match (str,sm,str_expr); regex_match ( str.cbegin(), str.cend(), sm, str_expr); cout << "String:range, size:" << sm.size() << " matches\"; regex_match ( mystr, cm, str_expr, regex_constants::match_default ); cout << "the matches are: "; for (unsigned i=0; i<sm.size(); ++i) { cout << "[" << sm[i] << "] "; } cout << endl; return 0;}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
在上面的程序中,首先,我们使用regex_match
函数将字符串" softwareTesting "
与正则表达式" (soft)(.*) "
匹配。随后,我们还通过向regex_match
传递字符串对象、范围等来演示regex_match
的不同变体。
3.2 regex_search()
函数regex_search()
用于在字符串中搜索匹配正则表达式的模式。
考虑下面的c++程序,它显示了regex_search()
的用法。
#include <iostream> #include <regex> #include<string.h> using namespace std; int main() { //string to be searched string mystr = "She sells_sea shells in the sea shore"; // regex expression for pattern to be searched regex regexp("s[a-z_]+"); // flag type for determining the matching behavior (in this case on string objects) smatch m; // regex_search that searches pattern regexp in the string mystr regex_search(mystr, m, regexp); cout<<"String that matches the pattern:"<<endl; for (auto x : m) cout << x << " "; return 0; }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
我们先指定一个字符串,然后使用regex
对象指定一个正则表达式。这个字符串和regex
连同smatch
标志类型一起被传递给regex_search
函数。该函数在输入字符串中搜索第一个出现的模式,并返回匹配的字符串。
3.3 regex_replace()
函数regex_replace()
用于用字符串替换与正则表达式匹配的模式。
让我们使用一个c++程序来演示regex_replace()
函数。
#include <iostream> #include <string> #include <regex> #include <iterator> using namespace std; int main() { string mystr = "This is software testing Help portal \"; cout<<"Input string: "<<mystr<<endl; // regex to match string beginning with 'p' regex regexp("p[a-zA-z]+"); cout<<"Replace the word 'portal' with word 'website' : "; // regex_replace() for replacing the match with the word 'website' cout << regex_replace(mystr, regexp, "website"); string result; cout<<"Replace the word 'website' back to 'portal': "; // regex_replace( ) for replacing the match back with 'portal' regex_replace(back_inserter(result), mystr.begin(), mystr.end(), regexp, "portal"); cout << result; return 0; }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
这里,我们有一个输入字符串。我们提供了一个正则表达式来匹配以 p
开头的字符串。然后我们将匹配的单词替换为website
。接下来,我们将website
替换回portal
。
4.C++输入验证
我们已经讨论了使用regex
进行模式匹配的主要函数模板。值得注意的是,regex
服务的主要目的是输入验证。可以使用regex
表达式验证从标准输入设备输入的输入。
检查下面的程序,以演示如何使用regex
验证传入的数据。
#include <iostream>#include <regex>#include <string>using namespace std; int main(){ string input; regex integer_expr("(\\+|-)?[[:digit:]]+"); //As long as the input is correct ask for another number while(true) { cout<<"Enter the input: "; cin>>input; if(!cin) break; //Exit when the user inputs q if(input=="q") break; if(regex_match(input,integer_expr)) cout<<"Input is an integer"<<endl; else {cout<<"Invalid input : Not an integer"<<endl;} }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
这个程序匹配用户输入的输入,以验证它是否是整数。上面的输出显示,当输入一个整数时,它会给出一个适当的消息,而当输入任何其他数据时,它会将该消息作为无效输入。
5.总结
Regex
在搜索引擎中用于搜索模式,搜索和替换应用程序的对话框,如字处理器和文本编辑器。Regex
也用于UNIX实用程序,如sed、awk
以及程序的词法分析。
在本教程中,我们已经看到了用于匹配、搜索和替换模式的函数。使用这些函数,我们基本上可以开发一个高效的应用程序,使用regex
实现所需的功能。
Regex
允许通过修补模式有效地验证输入或搜索并替换字符串,这可以使用很少的行来完成。