- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有以下字符串:
std::string s("server ('m1.labs.teradata.com') username ('use\\')r_*5') password('u\" er 5') dbname ('default')");
我使用了以下代码:
int main() {
std::regex re(R"('[^'\\]*(?:\\[\s\S][^'\\]*)*')");
std::string s("server ('m1.labs.teradata.com') username ('use\\')r_*5') password('u\" er 5') dbname ('default')");
unsigned count = 0;
for(std::sregex_iterator i = std::sregex_iterator(s.begin(), s.end(), re);
i != std::sregex_iterator();
++i)
{
std::smatch m = *i;
cout << "the token is"<<" "<< m.str() << endl;
count++;
}
cout << "There were " << count << " tokens found." << endl;
return 0;
以上字符串的输出是:
the token is 'm1.labs.teradata.com'
the token is 'use\')r_*5'
the token is 'u" er 5'
the token is 'default'
There were 4 tokens found.
现在如果上面代码中提到的字符串s是
std::string s("server ('m1.labs.ter\'adata.com') username ('use\\')r_*5') password('u\" er 5') dbname ('default')");
输出变为:
the token is 'm1.labs.ter'
the token is ') username ('
the token is ')r_*5'
the token is 'u" er 5'
the token is 'default'
There were 5 tokens found.
现在两个字符串的输出不同:预期的输出是“提取括号和单引号之间的所有内容,即
the token is 'm1.labs.teradata.com'
the token is 'use\')r_*5'
the token is 'u" er 5'
the token is 'default'
There were 4 tokens found
我在代码中提到的正则表达式能够正确提取但无法转义“单引号”。它能够转义 ",) 等但不能转义单引号。可以修改正则表达式以产生我需要的输出吗?提前致谢。
最佳答案
您使用的是我昨天通过评论分享的正确正则表达式。它匹配可能在内部转义了单引号的单引号字符串文字。
std::regex re(R"('([^'\\]*(?:\\[\s\S][^'\\]*)*)')");
std::string s("server ('m1.labs.teradata.com') username ('u\\'se)r_*5') password('uer 5') dbname ('default')");
unsigned count = 0;
for(std::sregex_iterator i = std::sregex_iterator(s.begin(), s.end(), re);
i != std::sregex_iterator();
++i)
{
std::smatch m = *i;
cout << "the token is"<<" "<< m.str(1) << endl;
count++;
}
cout << "There were " << count << " tokens found." << endl;
这里是 my C++ demo
请注意,文字字符串 ('u\'se)r_*5')
应该像这样使用常规字符串文字定义,其中支持转义序列 where文字反斜杠应使用 \\
:
"('u\\'se)r_*5')"
或使用原始字符串文字,其中反斜杠表示文字反斜杠:
R"(('u\'se)r_*5'))"
R"(...)"
形成原始字符串文字。
图案细节:
'
- 单引号[^'\\]*
- 除了单引号和反斜杠之外的 0+ 个字符(?:\\[\s\S][^'\\]*)*
- 零个或多个序列:
\\[\s\S]
- 任何反斜杠转义字符[^'\\]*
- 除了 '
和 \
'
- 单引号。请注意,为了避免将第一个单引号匹配为转义引号,您需要调整表达式,如 this snippet :
std::regex re(R"((?:^|[^\\])(?:\\{2})*'([^'\\]*(?:\\[\s\S][^'\\]*)*)')");
std::string s("server ('m1.labs.teradata.com') username ('u\\'se)r_*5') password('uer 5') dbname ('default')");
unsigned count = 0;
for(std::sregex_iterator i = std::sregex_iterator(s.begin(), s.end(), re);
i != std::sregex_iterator();
++i)
{
std::smatch m = *i;
cout << "the token is"<<" "<< m.str(1) << endl;
count++;
}
cout << "There were " << count << " tokens found." << endl;
(?:^|[^\\])(?:\\{2})*
前缀将匹配字符串的开头或除 \
之外的任何字符> 然后是 2 个 \
的 0+ 序列,因此首先不会抓取任何转义的 '
。
最后,如果您只需要将匹配列表放入 vector 中,您可以使用
#include <iostream>
#include <string>
#include <vector>
#include <regex>
using namespace std;
int main() {
std::regex rx("'[^']*(?:''[^']*)*'");
std::string sentence("server ('m1.labs.\\''tera\"da ta.com') username ('us *(er'')5') password('uer 5') dbname ('default')");
std::vector<std::string> names(std::sregex_token_iterator(sentence.begin(), sentence.end(), rx),
std::sregex_token_iterator());
for( auto & p : names ) cout << p << endl;
return 0;
}
参见 C++ demo .
关于c++ - 正则表达式中的转义 (\') 单引号在两个单引号之间采用字符串。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45207532/
考虑以下代码: template struct list { template list(Args...) { static_assert(sizeof..
考虑以下代码: template struct list { template list(Args...) { static_assert(sizeof..
最近才开始学习"new"OpenGL(可编程而不是固定功能,我从 Nehe 教程中学到的),我想知道自从 OpenGL 4 发布以来学习 OpenGL 3 是否真的有用。 我问的原因是因为我想知道能够
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我想了解如何操作特征向量/矩阵。我想实现最小二乘高斯牛顿算法(因此我学习使用 Eigen 库)。我有一个 1x6 的参数 vector ,每次迭代都需要更新它们。现在,我只想弄清楚函数如何将 vect
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭 5 年前。 Improv
我发现编写适用于Enums的静态方法非常困难。这是一个非常人为的示例,但假设您想要编写一个方法,该方法采用 Enum 常量并返回下一个声明的常量。我发现(大约一个小时后)你可以按如下方式进行。它可以工
我正在尝试编写一个函数,在某些条件下,将指向结构的指针更改为指向不同的结构。 我的限制是我想保留初始函数签名,该签名将指向指针(而不是特定结构类型)的通用指针作为参数。 这行不通: [nav] In
我正在尝试将 Keras 示例改编为 VAE https://blog.keras.io/building-autoencoders-in-keras.html 我修改了代码,使用有噪声的 mnist
自 JPA 2.0 以来,关系上有 orphanRemoval 属性,它极大地简化了父子关系的更新,并且与级联删除一起允许删除树的整个分支并轻松删除它。 但是,也有一些情况可能被标记为“收养”,即您将
我正在尝试编写一个类,它能够在以后及时调用不带参数的 lambda。我期待 C++17 类模板参数推导以避免需要工厂函数。但是,尝试在不指定类型的情况下实例化对象会失败。我可以很好地使用工厂功能,但我
我怎样才能避免并非所有控制路径都在此处返回容器的事实: enum Type {Int, String}; Container containerFactory(Type
我开始学习 C++ 和 STL。 我有一个问题: 写一个函数模板palindrome,接受一个 vector 参数并返回true或false来检查 vector 是否是回文(12321是回文,1234
我一直在尝试获取一个条目值(代码中的 S1)以将其自身设置为一个值(_attributes 字典中的 STR),但我就是无法让它工作。我想让它成为一个最终的顶层循环,但我在这方面一步一步来,因为我是一
我想做同样的事情 How do I get the number of days between two dates in JavaScript? 但我想对此日期格式执行相同操作:2000-12-31
我想编写一个带有构造函数的 C++ 类,该构造函数将 auto_ptr 作为其参数,以便我可以将类实例从 auto_ptr 初始化为另一个实例: #include class A { public:
我需要一种方法,我可以在其中获取二维数组中的输入并以最快的方式之一对其进行逐行排序。我尝试使用 Insertion Sort 同时获取 Input 和 Sort it。我使用的第二件事是我单独为一行取
好的,我已经阅读了一些关于 IDisposable 最佳实践的文章,我想我基本上明白了(终于)。 我的问题与从 IDisposable 基类继承有关。我看到的所有示例都在子类中一遍又一遍地编写相同的代
定义类时,以下是否有效? T(const T&&) = default; 我正在阅读移动构造函数 here并且它解释了如何仍然可以隐式声明默认值: A class can have multiple
我想使用 LoadLibrary 开发一个插件系统。 我的问题是:我希望我的函数采用 const char* 而 LoadLibrary 采用 LPCTSTR。 我有一个聪明的想法来做(LPCSTR)
我是一名优秀的程序员,十分优秀!