C++ 各种数字类型的正则表达式

简介: C++ 各种数字类型的正则表达式

C++发展简史


1998年,C++标准委员会成立,第一版ISO/IEC 14882:1998公开,即C++98。


2003年,C++标准委员会通过第二版标准ISO/IEC 14882:2003,简称C++03。


2011年,很多年的发展终于通过C++11,ISO/IEC 14882:2011,别名C++0x。


2014年,C++标准委员会一致通过C++14。ISO/IEC 14882:2014,别名C++1y。


2017年,C++又一次重大更新,官方名称 ISO/IEC 14882:2017, 别名C++1z。


2020年9月4日,C++20的国际标准草案投票结束,年底正式发布,别名C++2a。





C++ 有这么多的版本,可是对像我一样的广大初学者来说,可能C++11都还没有普及了。最近我正在学用的正则表达式正是从C++11开始支持的,头文件 <regex>,不再是只能用第三方 "boost/regex.hpp" 等等的库了。而我用的编译器是 TDM-GCC 4.9.2 64-bit,需要在菜单命令:“工具--编译选项--编译器”中设置配置,编译时加入以下命令文本框里添加 “-std=c++11”,如下图所示:


20210117214814499.png


刚接触正则表达式,初步学了点皮毛记录一下。直接上代码:

#include <iostream>
#include <string>
#include <vector>
#include <regex>
using namespace std;
int regexSplit(string&,const string,vector<string>&,int);
int main(void)
{
  vector <string> vect;
  string str = "(12.3e+10-0.018e-5)+(11.006-7.)+.89";
  string reg[11]={
    "(\\d+)",       /*整数,包括0开头的 */ 
    "([1-9]\\d*)",      /*错:整数,但取不到0 */ 
    "(0|[1-9]\\d*)",    /*全部整数 */ 
    "(\\d+\\.\\d+)",    /*小数不包括整数 */ 
    "(\\d*\\.?\\d+)",   /*错:整数或小数,但包括7. */ 
    "(\\d+\\.?\\d*)",   /*错:整数或小数,但包括.89 */ 
    "(\\d+|\\d+\\.\\d+)", /*整数或小数 */ 
    "-?(\\d+|\\d+\\.\\d+)", /*正数或负数 */ 
    "\\([^()]*\\)",     /*匹配成对的括号 */ 
    "-?(\\d+\\.\\d+)e[+-]\\d+", /*科学记数法 */  
    "-?((\\d+|\\d+\\.\\d+)|(\\d+\\.\\d+)e[+-]\\d+)" /*实数 */
    };
  cout<<str<<endl<<"--------------"<<endl;
  for (auto a:reg){
    regexSplit(str,a,vect,0);
    cout<<"pattern:"<<a<<endl<<"string: ";
    for(auto v:vect) cout<<v<<" ";
    vect.clear(); //重要,否则后一次的循环取到的值会累积到vect中。
    cout<<endl<<"=============="<<endl;
  }
  return 0;
 }
int regexSplit(string &str,const string str_reg,vector<string>&vect,int pos)
{
  if (pos!=-1) pos=0;  //pos=0 匹配到的位置,pos=-1匹配位置的前一字串 
  regex Pattern(str_reg); 
    sregex_token_iterator it(str.begin(),str.end(),Pattern, pos); 
    sregex_token_iterator end;
    for(;it!=end;++it,i++) vect.push_back(*it); 
    return vect.size();  //if 0 没有匹配到,else 匹配到的个数
 } 


输出结果:

(12.3e+10-0.018e-5)+(11.006-7.)+.89
--------------
pattern:(\d+)
string: 12 3 10 0 018 5 11 006 7 89
==============
pattern:([1-9]\d*)
string: 12 3 10 18 5 11 6 7 89
==============
pattern:(0|[1-9]\d*)
string: 12 3 10 0 0 18 5 11 0 0 6 7 89
==============
pattern:(\d+\.\d+)
string: 12.3 0.018 11.006
==============
pattern:(\d*\.?\d+)
string: 12.3 10 0.018 5 11.006 7 .89
==============
pattern:(\d+\.?\d*)
string: 12.3 10 0.018 5 11.006 7. 89
==============
pattern:(\d+|\d+\.\d+)
string: 12.3 10 0.018 5 11.006 7 89
==============
pattern:-?(\d+|\d+\.\d+)
string: 12.3 10 -0.018 -5 11.006 -7 89
==============
pattern:\([^()]*\)
string: (12.3e+10-0.018e-5) (11.006-7.)
==============
pattern:-?(\d+\.\d+)e[+-]\d+
string: 12.3e+10 -0.018e-5
==============
pattern:-?((\d+|\d+\.\d+)|(\d+\.\d+)e[+-]\d+)
string: 12.3e+10 -0.018e-5 11.006 -7 89
==============
--------------------------------
Process exited after 0.5831 seconds with return value 0
请按任意键继续. . .


附录:


特殊字符:

characters    description    matches
.    not newline    any character exceptline terminators(LF, CR, LS, PS).
\t    tab (HT)    a horizontal tab character (same as\u0009).
\n    newline (LF)    a newline (line feed) character (same as\u000A).
\v    vertical tab (VT)    a vertical tab character (same as\u000B).
\f    form feed (FF)    a form feed character (same as\u000C).
\r    carriage return (CR)    a carriage return character (same as\u000D).
\cletter    control code    a control code character whosecode unit valueis the same as the remainder of dividing thecode unit valueofletterby 32.
For example:\cais the same as\u0001,\cbthe same as\u0002, and so on...
\xhh    ASCII character    a character whosecode unit valuehas an hex value equivalent to the two hex digitshh.
For example:\x4cis the same asL, or\x23the same as#.
\uhhhh    unicode character    a character whosecode unit valuehas an hex value equivalent to the four hex digitshhhh.
\0    null    a null character (same as\u0000).
\int    backreference    the result of the submatch whose opening parenthesis is theint-th (intshall begin by a digit other than0). Seegroupsbelow for more info.
\d    digit    a decimal digit character
\D    not digit    any character that is not a decimal digit character
\s    whitespace    a whitespace character
\S    not whitespace    any character that is not a whitespace character
\w    word    an alphanumeric or underscore character
\W    not word    any character that is not an alphanumeric or underscore character
\character    character    the charactercharacteras it is, without interpreting its special meaning within a regex expression.
Anycharactercan be escaped except those which form any of the special character sequences above.
Needed for:^ $ \ . * + ? ( ) [ ] { } |
[class]    character class    the target character is part of the class
[^class]    negated character class    the target character is not part of the class

数量:

characters    times    effects
*    0 or more    The preceding atom is matched 0 or more times.
+    1 or more    The preceding atom is matched 1 or more times.
?    0 or 1    The preceding atom is optional (matched either 0 times or once).
{int}    int    The preceding atom is matched exactlyinttimes.
{int,}    intor more    The preceding atom is matchedintor more times.
{min,max}    betweenminandmax    The preceding atom is matched at leastmintimes, but not more thanmax.

分组:

characters    description    effects
(subpattern)    Group    Creates a backreference.
(?:subpattern)    Passive group    Does not create a backreference.

其他:

characters    description    condition for match
^    Beginning of line    Either it is the beginning of the target sequence, or follows aline terminator.
$    End of line    Either it is the end of the target sequence, or precedes aline terminator.
|    Separator    Separates two alternative patterns or subpatterns..




目录
相关文章
|
4月前
|
存储 编译器 程序员
C++类型参数化
【10月更文挑战第1天】在 C++ 中,模板是实现类型参数化的主要工具,用于编写能处理多种数据类型的代码。模板分为函数模板和类模板。函数模板以 `template` 关键字定义,允许使用任意类型参数 `T`,并在调用时自动推导具体类型。类模板则定义泛型类,如动态数组,可在实例化时指定具体类型。模板还支持特化,为特定类型提供定制实现。模板在编译时实例化,需放置在头文件中以确保编译器可见。
52 11
|
5月前
|
安全 程序员 C语言
C++(四)类型强转
本文详细介绍了C++中的四种类型强制转换:`static_cast`、`reinterpret_cast`、`const_cast`和`dynamic_cast`。每种转换都有其特定用途和适用场景,如`static_cast`用于相关类型间的显式转换,`reinterpret_cast`用于低层内存布局操作,`const_cast`用于添加或移除`const`限定符,而`dynamic_cast`则用于运行时的类型检查和转换。通过具体示例展示了如何正确使用这四种转换操作符,帮助开发者更好地理解和掌握C++中的类型转换机制。
|
6月前
|
C++
使用 QML 类型系统注册 C++ 类型
使用 QML 类型系统注册 C++ 类型
164 0
|
7月前
|
编译器 C++ 运维
开发与运维函数问题之函数的返回类型如何解决
开发与运维函数问题之函数的返回类型如何解决
49 6
|
7月前
|
安全 编译器 C++
C++一分钟之-模板元编程实例:类型 traits
【7月更文挑战第15天】C++的模板元编程利用编译时计算提升性能,类型traits是其中的关键,用于查询和修改类型信息。文章探讨了如何使用和避免过度复杂化、误用模板特化及依赖特定编译器的问题。示例展示了`is_same`类型trait的实现,用于检查类型相等。通过`add_pointer`和`remove_reference`等traits,可以构建更复杂的类型转换逻辑。类型traits增强了代码效率和安全性,是深入C++编程的必备工具。
112 12
|
7月前
|
C++
C++一分钟之-类型别名与using声明
【7月更文挑战第20天】在C++中,类型别名和`using`声明提升代码清晰度与管理。类型别名简化复杂类型,如`using ComplexType = std::vector&lt;std::shared_ptr&lt;int&gt;&gt;;`,需注意命名清晰与适度使用。`using`声明引入命名空间成员,避免`using namespace std;`全局污染,宜局部与具体引入,如`using math::pi;`。恰当应用增强代码质量,规避常见陷阱。
110 5
|
6月前
|
存储 C++
【C/C++学习笔记】string 类型的输入操作符和 getline 函数分别如何处理空白字符
【C/C++学习笔记】string 类型的输入操作符和 getline 函数分别如何处理空白字符
69 0
|
6月前
|
设计模式 安全 IDE
C++从静态类型到单例模式
C++从静态类型到单例模式
52 0
|
7月前
|
C++ 开发者
C++一分钟之-概念(concepts):C++20的类型约束
【7月更文挑战第4天】C++20引入了Concepts,提升模板编程的类型约束和可读性。概念定义了模板参数需遵循的规则。常见问题包括过度约束、约束不完整和重载决议复杂性。避免问题的关键在于适度约束、全面覆盖约束条件和理解重载决议。示例展示了如何用Concepts限制模板函数接受的类型。概念将增强模板的安全性和灵活性,但需谨慎使用以防止错误。随着C++的发展,Concepts将成为必备工具。
156 2
|
8月前
|
编译器 程序员 语音技术
C++的超20种函数类型分享
C++超20种函数类型:编程语言规定规则,编译器实现预定规则