I have this code:
我有这个代码:
std::smatch m;
std::string dataType = "type = struct A {\nint a;\nint b;\nint c; }";
std::regex_search(dataType, m, std::regex("(= )(.*)( [{]|\n|$)"));
std::cout << m.str(2) << std::endl;
The problem is that it returns the longest match, but I need the smallest. The output is:
问题是它返回最长的匹配,但我需要最小的匹配。输出是:
struct A {\n int a;\n int b;\n int c; }
But it needs to be:
但它必须是:
struct A
结构A.
How can I get the result I want?
我怎样才能得到我想要的结果?
3 个解决方案
#1
0
To get "struct A" from your text, the regex to be used is:
要从文本中获取“struct A”,要使用的正则表达式是:
\=\s(\w+\s\w+)
if you have other cases, please give more specifications or examples of your input and how your output should look like.
如果您有其他情况,请提供更多的输入规格或示例以及输出的外观。
Edit: thanks to user3259253, the correct solution is:
编辑:感谢user3259253,正确的解决方案是:
\\=\\s(\\w+(\\s\\w+)?)
#2
2
You could change .*
to .*?
.
你可以改变。*到。*?。
Read up on greediness.
阅读贪婪。
(Did you really mean to put a literal newline in the regex? You probably want to make that \\n
.)
(你真的是想在正则表达式中添加一个字面换行符吗?你可能想要把它作为\ n。)
#3
0
Right, use this:
对,用这个:
std::regex_search(dataType, m, std::regex("(= )(.*)([\\{]|\\n)$"));
However, if you only what to capture what's between the = sign and the curly bracket/end of line, you don't need so many () groups. This would be enough:
但是,如果您只想捕获=符号和花括号/行尾之间的内容,则不需要这么多()组。这就足够了:
std::regex_search(dataType, m, std::regex("= (.*)[\\{]|\\n$"));
std::cout << m.str(1) << std::endl;
Note that here we're catching the text as the first entry, m.str(1), instead of the second, because we have eliminated "(= )"
请注意,这里我们将文本作为第一个条目m.str(1)而不是第二个,因为我们已经删除了“(=)”
#1
0
To get "struct A" from your text, the regex to be used is:
要从文本中获取“struct A”,要使用的正则表达式是:
\=\s(\w+\s\w+)
if you have other cases, please give more specifications or examples of your input and how your output should look like.
如果您有其他情况,请提供更多的输入规格或示例以及输出的外观。
Edit: thanks to user3259253, the correct solution is:
编辑:感谢user3259253,正确的解决方案是:
\\=\\s(\\w+(\\s\\w+)?)
#2
2
You could change .*
to .*?
.
你可以改变。*到。*?。
Read up on greediness.
阅读贪婪。
(Did you really mean to put a literal newline in the regex? You probably want to make that \\n
.)
(你真的是想在正则表达式中添加一个字面换行符吗?你可能想要把它作为\ n。)
#3
0
Right, use this:
对,用这个:
std::regex_search(dataType, m, std::regex("(= )(.*)([\\{]|\\n)$"));
However, if you only what to capture what's between the = sign and the curly bracket/end of line, you don't need so many () groups. This would be enough:
但是,如果您只想捕获=符号和花括号/行尾之间的内容,则不需要这么多()组。这就足够了:
std::regex_search(dataType, m, std::regex("= (.*)[\\{]|\\n$"));
std::cout << m.str(1) << std::endl;
Note that here we're catching the text as the first entry, m.str(1), instead of the second, because we have eliminated "(= )"
请注意,这里我们将文本作为第一个条目m.str(1)而不是第二个,因为我们已经删除了“(=)”