正则表达式 - 请教一个 c++11的 正则语法
怪我咯
怪我咯 2017-04-17 15:25:28
0
3
587

R"xxx()xxx": xxx可以被替换成任何字符串,不可以包含括号和双引号,而且你需要保证两边是一样的。如果你这样写,那么括号里面的任何字符都不会被转义,甚至是换行符也没有问题。这种字符串语法用来写正则表达式就特别的方便。

可以详细说下用法吗, 感觉不是很理解.

怪我咯
怪我咯

走同样的路,发现不同的人生

reply all(3)
阿神

I don’t know whether you don’t understandRor notxxx, so I’ll tell you everything

Let me start with R, which represents Raw String

For example, the following text

 abc
     efg\n

Before there was Raw String, you had to write like this

"abc\n\tefg\n"

Special symbols in the string must be escaped

But after having Raw String, you can write like this

R"(abc
    efg\n)"

That is, the text inside the brackets Raw String is What you see is what you get

Maybe you are wondering, why do you need a pair of parentheses?

Think about the text

abc"efg

If there are no brackets, it will be written as R"abc"efg", so it is impossible to determine which is the paired double quote
, so brackets need to be introduced to delimit

After introducing brackets, what if there are quotation marks + brackets in the original text?
For example

abc)"efg

According to the above writing method, it needs to be written as R"(abc)"efg)", which will obviously lead to grammatical errors and it will be impossible to determine which pair is )"
Obviously, when the text contains )" , () is not enough to delimit

Therefore, you need to customize delimiter (delimiter) , which is the xxx(...)xxx in your question. The string above
can be written as R"xxx(abc)"efg)xxx", and
is compiled like this The programmer will be able to tell that everything contained in xxx(...)xxx is the original text.
Of course, xxx can be customized by you. You can also write it as ab(...)ab

巴扎黑

This is raw string literals in C++11

#include <iostream>

int main()
{
    std::cout << R"delimiter(\non\t-\t"escape")delimiter" << std::endl;
    return 0;
}

Output

\non\t-\t"escape"

That is, the characters between the brackets will not be escaped and will be output as is.
Because "" is used frequently in regular expressions, this method avoids the problem of converting "" into "\".
For example:

"('(?:[^\']|\.)*'|"(?:[^\"]|\.)*")|"

If you don’t use this method, you need to write it like this:

"('(?:[^\\']|\\.)*'|\"(?:[^\\\"]|\\.)*\")|"
阿神

The purpose of custom split string: identify where the raw string ends.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template