所以我有一点问题,我想在C ++中实现这一点,但我不知道该怎么做:
Given是一个包含随机数字,符号和字母的字符串:
std::string = "1653gbdtsr362g2v3f3t52bv^hdtvsbjj;hdfuue,9^1dkkns";
现在我正在尝试查找所有^
个字符,如果这些字符后跟0到9之间的数字,请删除^
和数字,以便:
"^1ghhu^7dndn^g"
变为:
"ghhudndn^g"
我知道如何从字符串中查找和替换/删除字符,但我不知道如何检查它是否以非硬编码方式后跟数字。任何帮助表示赞赏。
答案 0 :(得分:1)
我会这样做:
#include <iostream>
#include <string>
#include <utility>
#include <iterator>
template<class Iter, class OutIter>
OutIter remove_escaped_numbers(Iter first, Iter last, OutIter out) {
for ( ; first != last ; )
{
auto c = *first++;
if (c == '^' && first != last)
{
c = *first++;
if (std::isdigit(c))
continue;
else {
*out++ = '^';
*out++ = c;
}
}
else {
*out++ = c;
}
}
return out;
}
int main()
{
using namespace std::literals;
auto input = "^1ghhu^7dndn^g"s;
auto output = std::string{};
remove_escaped_numbers(input.begin(), input.end(), std::back_inserter(output));
std::cout << output << std::endl;
}
或者这样:
#include <iostream>
#include <regex>
int main()
{
using namespace std::literals;
auto input = "^1ghhu^7dndn^g"s;
static const auto repl = std::regex { R"___(\^\d)___" };
auto output = std::regex_replace(input, repl, "");
std::cout << output << std::endl;
}
答案 1 :(得分:1)
std::string s = "^1ghhu^7dndn^g";
for (int i = 0; i < s.length() - 1; ++i)
{
if (s[i] == '^' && std::isdigit(s[i + 1]))
{
s.erase(i, 2);
--i;
}
}
这需要包括:
#include <string>
#include <cctype>
答案 2 :(得分:1)
使用std :: stringstream的解决方案,并返回清除了插入符号的输入字符串。
#include <iostream>
#include <sstream>
#include <cctype>
int t404()
{
std::stringstream ss;
std::string inStr("1653gbdtsr362g2v3f3t52bv^hdtvsbjj;hdfuue,9^1dkkns");
for (size_t i = 0; i<inStr.size(); ++i)
{
if(('^' == inStr[i]) && isdigit(inStr[i+1]))
{
i += 1; // skip over caret followed by single digit
}
else
{
ss << inStr[i];
}
}
std::cout << inStr << std::endl; // compare input
std::cout << ss.str() << std::endl; // to results
return 0;
}
输出:
1653gbdtsr362g2v3f3t52bv ^ hdtvsbjj; hdfuue,9 ^ 1dkkns 1653gbdtsr362g2v3f3t52bv ^ hdtvsbjj; hdfuue,9dkkns
答案 3 :(得分:0)
你可以简单地循环遍历字符串并在跳过不需要的字符时复制它。这是一个可能的功能:
std::string filterString (std::string& s) {
std::string result = "";
std::string::iterator it = s.begin();
char c;
while (it != s.end()) {
if (*it == '^' && it != s.end() && (it + 1) != s.end()) {
c = *(it + 1);
if(c >= '0' && c <= '9') {
it += 2;
continue;
}
}
result.push_back(*it);
++ it;
}
return result;
}
答案 4 :(得分:0)
一个强大的解决方案是使用C ++ 11带来的regex library。
std::string input ("1653gbdtsr362g2v3f3t52bv^hdtvsbjj;hdfuue,9^1dkkns");
std::regex rx ("[\\^][\\d]{1}"); // "[\^][\d]{1}"
std::cout << std::regex_replace(input,rx,"woot");
>> 1653gbdtsr362g2v3f3t52bv^hdtvsbjj;hdfuue,9wootdkkns
这会找到一个&#34; ^&#34;字符([\^]
)后跟1({1}
)个数字([\d]
),并用"woot"
替换所有出现。
答案 5 :(得分:0)
希望此代码可以解决您的问题:
@fm_reports = FmReport.find_by_sql(crosstab_query)
field_names = @fm_reports.first.attributes.keys
输出:
#include <iostream>
#include <string>
int main()
{
std::string str = "^1ghhu^7dndn^g";
std::string::iterator first, last;
for ( std::string::iterator it=str.begin(); it!=str.end(); ++it)
{
if(*it == '^')
{
first = it;
it++;
while(isdigit(*it))
{
it++;
}
last = it - 1;
if(first != last)
{
if((last + 1) != str.end())
{
str.erase(first, last + 1);
}
else
{
str.erase(first, str.end());
break;
}
}
}
}
std::cout<< str << std::endl;
return 0;
}