C ++正则表达式允许用点分隔的数字
c++ regexp allowing digits separated by dot
我需要 rexexp 允许在一行中最多用点分隔两位数字,例如 1.2 或 1.2.3 或 1.2.3.45 等,但不允许 1234 或 1.234 等。我正在尝试这个"^[\d{1,2}。+",但它允许所有数字。怎么了?
你可以试试这个:
^d{1,2}(.d{1,2})+$
正则表达式 101 演示
解释:
- 字符串的开头
^
d{1,2}
后跟一位或两位数字- 捕获组
(
开始 .d{1,2}
后跟一个点和一个或两个数字)
捕获组的末尾+
指示上一个捕获组重复 1 次或多次- 字符串的末尾
$
示例C++源(在此处运行(:
#include <regex>
#include <string>
#include <iostream>
using namespace std;
int main()
{
string regx = R"(^d{1,2}(.d{1,2})+$)";
string input = "1.2.346";
smatch matches;
if (regex_search(input, matches, regex(regx)))
{
cout<<"match found";
}
else
cout<<"No match found";
return 0;
}
我认为最后一个不应该超过 2 位数字。
(d{1,2}.)+d{1,2}(?=b)
相关文章:
- 如何在 C++ 中将文件中的逗号分隔数字读取到数组中?
- 分隔整数 (C++) 的数字
- 如何从文件中读取用空格分隔的数字
- C ++正则表达式允许用点分隔的数字
- 读取以逗号分隔的数字,并将其写入以制表符分隔的文件
- C++ 从文件中读取 int,每个数字用逗号分隔
- 我如何使用CIN输入整数,每个数字都被逗号分隔
- 如何从逗号分隔的数字对中填充 2 个向量 C++.
- 为什么我可以在 C++ 中用撇号分隔数字,但不能用 C 分隔数字
- 用字符分隔数字
- 逗号分隔的数字在模拟器上正确显示,但在iPad上没有显示
- 在 cpp 中使用 getline 输入数组中的空格分隔数字
- 给定分隔成数组元素的数字,如何在 int 中重新组合数字
- 如果字符串中没有空格,如何解析带有数字和字符的字符串并分隔数字和字符
- 扫描以空格分隔的数字
- 解析逗号分隔的数字(C++)
- C++ - 如何将空格分隔的数字输入数组
- 递增由不同分隔符分隔的字符数组中的数字
- 解析来自 getline 的空格分隔数字
- 如何在C或c++代码中分隔数字常量(例如10,000)中的数字