首页 > 后端开发 > C++ > 正文

使用正则表达式的C++程序打印每个单词的首字母

王林
发布: 2023-08-26 21:33:17
转载
1123 人浏览过

使用正则表达式的C++程序打印每个单词的首字母

A useful tool for string operations is regex. This may be found in virtually all high-level 当前的编程语言,包括C++。正则表达式(Regex)被用作 通用搜索模式。例如,通过构建一个简单的字符串 被称为正则表达式,我们可以使用至少实现密码验证逻辑 一个大写字母,一个小写字母,一个数字,一个特殊字符,并且总长度至少为 8个字符。

In this tutorial, we'll look at how to use C++ to display only the first letters of words included 在指定的字符串内。在这里,我们将看一个使用空格来分隔单词的句子 无论字符是大写还是小写,计算机都会读取 将字符串使用正则表达式分割,并返回每个单词的第一个字符。

To use regular expressions, we need to import the regex library using the ‘regex’ header. To 使用正则表达式,我们需要以下语法 -

语法(创建正则表达式对象)

regex obj_name( <regular expression> )
登录后复制

After defining regex, we can use them in multiple ways. We will see our intended approach 在下面。现在要从单词中读取第一个字符,正则表达式的语法将会是这样的 下面的内容为:

语法(读取单词的第一个字符)

\b[a-zA-Z]
登录后复制

在这里,‘b’表示单词的开头。[a-zA-Z]表示大写或小写字母 lowercase letters which are in the range of ‘a’ to ‘z’ or ‘A’ to ‘Z’. And only one of them is taken. 现在让我们看一下正在用于读取所有选定匹配项的迭代器对象 -

语法(正则表达式迭代器)

regex_token_iterator<string::iterator> iterator_name( <begin pointer of string>, <ending pointer of string>, <regular expression>, <submatch>);
登录后复制
在这个迭代器中,前两个参数是起始和结束指针的 string object. The third parameter is the given regular expression object which we have 在之前创建。第四个参数是子匹配。当子匹配为0时,它将 返回那些来自匹配的元素(在匹配时)的内容,当 submatch is -1, it represents where the matching is not done (reverse of the submatch 0). submatch为-1,表示匹配未完成的位置(与submatch 0相反)

算法

  • 将字符串 s 作为输入
  • define regular expression with 'b[a-zA-Z]'
  • 对s使用表达式进行匹配
  • 定义一个迭代器对象来仅读取匹配项
  • for each item in iterator object, do
    • 显示项目
  • 结束循环

Example

#include <iostream>
#include <regex>
using namespace std;
string solve( string s){
   string ret = "";
   regex e("\b[a-zA-Z]");
   regex_token_iterator<string::iterator> i(s.begin(), s.end(), e, 0);
   regex_token_iterator<string::iterator> end;
   while (i != end) {
      ret += (*i++);
      ret += ", ";
   }
   return ret;
}
int main(){
   string s = "A string to read only the first letter of words";
   cout << "Given String: " << s << endl;
   cout << "The first letter of each word: " << solve( s ) << endl;
   s = "Central Pollution Control Board";
   cout << "Given String: " << s << endl;
   cout << "The first letter of each word: " << solve( s ) << endl;
}
登录后复制

输出

Given String: A string to read only the first letter of words
The first letter of each word: A, s, t, r, o, t, f, l, o, w, 
Given String: Central Pollution Control Board
The first letter of each word: C, P, C, B, 
登录后复制

Conclusion

表达式在字符串中用于匹配常见模式 表达式库(正则表达式)在所有高级语言中都可用,包括Java,Python Javascript,Dart和C++。它有许多应用。一个正则表达式, reads the first character of each word has been defined in this article. We need an iterator ,它会逐个读取每个匹配的字符,以便循环遍历它们。

以上是使用正则表达式的C++程序打印每个单词的首字母的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:tutorialspoint.com
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!