C ++正则表达式替换第一个匹配项 [英] C++ regex replace first match
本文介绍了C ++正则表达式替换第一个匹配项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图编写一种类似于Java的 Matcher :: replaceFirst(字符串替换)
的方法。我知道如何用 std :: regex_replace
替换 std :: string
中的所有匹配项,但是我该如何只替换字符串中正则表达式的第一个匹配项?
I tried to write a method which acts like Java's Matcher::replaceFirst(String replacement)
. I know how to replace all matches in a std::string
, with std::regex_replace
, but how can I only replace the first match of a regex in a string?
推荐答案
您可以使用 std :: regex_replace
和一个附加标志 std :: regex_constants :: format_first_only
,以获得所需的结果。
You can use std::regex_replace
with an additional flag, std::regex_constants::format_first_only
, to get the result you want.
看看 http://en.cppreference.com/w/ cpp / regex / regex_replace 了解更多信息。
这篇关于C ++正则表达式替换第一个匹配项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文