使用std :: tr1 :: regex_iterator搜索并替换字符串中的出现。 [英] using std::tr1::regex_iterator to search and replace occurences in a string.

查看:130
本文介绍了使用std :: tr1 :: regex_iterator搜索并替换字符串中的出现。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用std :: tr1 :: regex_iterator来搜索和替换字符串?



我知道我可以使用std :: tr1 :: regex_replace替换所有出现的(或只有一个),但我需要用不同的东西替换我找到的每个模式,所以我需要迭代我的输入字符串并在运行中替换。



Is it possible to use std::tr1::regex_iterator to search and replace in a string ?

I know I can use std::tr1::regex_replace to replace all occurrences (or only one), but I need to replace each "pattern" that I find with something different, so I need to iterate my input string and replace on the fly.

std::string expression; // valid input string.
std::tr1::regex variableRegExpression; // valid regular expression.

std::tr1::sregex_iterator m1(
	expression.begin(), 
	expression.end(), 
	variableRegExpression);

std::tr1::sregex_iterator m2;
std::vector<std::string> tokens;
for (; m1 != m2; ++m1)
{
	std::string token = m1->str();
	// here, I want to replace token in the input string expression, but that will probably break the iterator.
}





以上代码将迭代我在字符串表达式。



我想做的是用其他东西替换每个事件。



我能用std :: tr1 :: regex_search和std :: regex_replace做一些循环,但它看起来并不干净:





The above code will iterate all occurrences of what I'm searching for in the string expression.

What I'd like to do would be to replace each occurrence with something else.

I was able to do that with some loops with std::tr1::regex_search and std::regex_replace, but it does not look "clean" :

std::string expression; // valid input string.
std::tr1::regex variableRegExpression; // valid regular expression.

// substitute variables.
bool found = true;
while (found)
{
	std::tr1::smatch matchResult;

	found = std::tr1::regex_search(
                expression, matchResult, variableRegExpression  );
        if ( found )
        {
                std::string newValue("Cloud1");
                // replace only first instance.
                // will change the input string.
                expression = std::tr1::regex_replace(
                        expression, variableRegExpression, newValue, 
                        std::tr1::regex_constants::format_first_only);

        }
        else
        {
                found = false;
        }
}







问题:

是否可以进行简单的搜索并替换为 regex_iterator



谢谢



Max。




Question :
Is it possible to do a simple search and replace with the regex_iterator ?

Thanks

Max.

推荐答案

我不是使用regex_iterator的专家,但我同意在飞行中进行替换打破迭代器。但是如何通过从原始字符串到匹配位置进行拼写来构建单独的结果字符串,然后附加替换字符串,然后将下一个复制位置标记移动到匹配结束...



通过这种方式,您不会干扰迭代器。这种方法几乎和就地替换一样有效。
I am no expert in using regex_iterator, but I would agree that doing the replacements on the fly will break the iterator. But how about building a separate result string by coyping from your original string up to the match position, then appending your replacement string, then moving the next copy position marker to the end of the match ...

In this manner you do not interfere with the iterator. And this method as almost as efficient as doing the replacements in-place.


这篇关于使用std :: tr1 :: regex_iterator搜索并替换字符串中的出现。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆