在句点和逗号之前从字符串中删除空格

时间:2022-12-28 20:22:33

I could have a string like:

我可以有一根弦

During this time , Bond meets a stunning IRS agent , whom he seduces .

在这段时间里,邦德遇到了一位美艳绝伦的国税局特工,他勾引了他。

I need to remove the extra spaces before the comma and before the period in my whole string. I tried throwing this into a char vector and only not push_back if the current char was " " and the following char was a "." or "," but it did not work. I know there is a simple way to do it maybe using trim(), find(), or erase() or some kind of regex but I am not the most familiar with regex.

我需要在逗号之前和在整个字符串的句点之前删除多余的空格。我尝试将它扔到一个char向量中,但是如果当前的char是“”,而下面的char是“。”或“,”,那么我就不回推_back,但是它不起作用。我知道有一种简单的方法可以使用trim()、find()或erase()或某种regex,但我对regex不是最熟悉的。

4 个解决方案

#1


1  

A solution could be (using regex library):

解决方案可以是(使用regex库):

std::string fix_string(const std::string& str) {
  static const std::regex rgx_pattern("\\s+(?=[\\.,])");
  std::string rtn;
  rtn.reserve(str.size());
  std::regex_replace(std::back_insert_iterator<std::string>(rtn),
                     str.cbegin(),
                     str.cend(),
                     rgx_pattern,
                     "");
  return rtn;
}

This function takes in input a string and "fixes the spaces problem".

此函数接受输入字符串并“修复空格问题”。

Here a demo

这里一个演示

#2


1  

On a loop search for string " ," and if you find one replace that to ",":

在循环搜索字符串“,”时,如果你找到一个将其替换为“,”:

std::string str = "...";
while( true ) {
    auto pos = str.find( " ," );
    if( pos == std::string::npos )
         break;
    str.replace( pos, 2, "," );
}

Do the same for " .". If you need to process different space symbols like tab use regex and proper group.

为“。”做同样的事。如果您需要处理不同的空格符号,如制表符,请使用regex和适当的组。

#3


1  

I don't know how to use regex for C++, also not sure if C++ supports PCRE regex, anyway I post this answer for the regex (I could delete it if it doesn't work for C++).

我不知道如何在c++中使用regex,也不确定c++是否支持PCRE regex,无论如何,我将这个答案发布到regex中(如果c++不起作用,我可以删除它)。

You can use this regex:

您可以使用这个regex:

\s+(?=[,.])

Regex demo

Regex演示

#4


0  

First, there is no need to use a vector of char: you could very well do the same by using an std::string.

首先,不需要使用char向量:您可以通过使用std::string来做同样的事情。

Then, your approach can't work because your copy is independent of the position of the space. Unfortunately you have to remove only spaces around the punctuation, and not those between words.

那么,你的方法就不能工作了,因为你的拷贝是独立于空间的位置的。不幸的是,你必须只删除标点符号周围的空格,而不是单词之间的空格。

Modifying your code slightly you could delay copy of spaces waiting to the value of the first non-space: if it's not a punctuation you'd copy a space before the character, otherwise you just copy the non-space char (thus getting rid of spaces.

稍微修改您的代码,您可以延迟等待第一个非空格值的空格的复制:如果不是标点,您可以在字符之前复制空格,否则您只需复制非空格字符(从而删除空格)。

Similarly, once you've copied a punctuation just loop and ignore the following spaces until the first non-space char.

类似地,一旦您复制了一个标点符号循环并忽略以下空格,直到第一个非空格字符。

I could have written code. It would have been shorter. But i prefer letting you finish your homework with full understanding of the approach.

我可以写代码。它会更短。但是我更愿意让你在充分理解方法的情况下完成作业。

#1


1  

A solution could be (using regex library):

解决方案可以是(使用regex库):

std::string fix_string(const std::string& str) {
  static const std::regex rgx_pattern("\\s+(?=[\\.,])");
  std::string rtn;
  rtn.reserve(str.size());
  std::regex_replace(std::back_insert_iterator<std::string>(rtn),
                     str.cbegin(),
                     str.cend(),
                     rgx_pattern,
                     "");
  return rtn;
}

This function takes in input a string and "fixes the spaces problem".

此函数接受输入字符串并“修复空格问题”。

Here a demo

这里一个演示

#2


1  

On a loop search for string " ," and if you find one replace that to ",":

在循环搜索字符串“,”时,如果你找到一个将其替换为“,”:

std::string str = "...";
while( true ) {
    auto pos = str.find( " ," );
    if( pos == std::string::npos )
         break;
    str.replace( pos, 2, "," );
}

Do the same for " .". If you need to process different space symbols like tab use regex and proper group.

为“。”做同样的事。如果您需要处理不同的空格符号,如制表符,请使用regex和适当的组。

#3


1  

I don't know how to use regex for C++, also not sure if C++ supports PCRE regex, anyway I post this answer for the regex (I could delete it if it doesn't work for C++).

我不知道如何在c++中使用regex,也不确定c++是否支持PCRE regex,无论如何,我将这个答案发布到regex中(如果c++不起作用,我可以删除它)。

You can use this regex:

您可以使用这个regex:

\s+(?=[,.])

Regex demo

Regex演示

#4


0  

First, there is no need to use a vector of char: you could very well do the same by using an std::string.

首先,不需要使用char向量:您可以通过使用std::string来做同样的事情。

Then, your approach can't work because your copy is independent of the position of the space. Unfortunately you have to remove only spaces around the punctuation, and not those between words.

那么,你的方法就不能工作了,因为你的拷贝是独立于空间的位置的。不幸的是,你必须只删除标点符号周围的空格,而不是单词之间的空格。

Modifying your code slightly you could delay copy of spaces waiting to the value of the first non-space: if it's not a punctuation you'd copy a space before the character, otherwise you just copy the non-space char (thus getting rid of spaces.

稍微修改您的代码,您可以延迟等待第一个非空格值的空格的复制:如果不是标点,您可以在字符之前复制空格,否则您只需复制非空格字符(从而删除空格)。

Similarly, once you've copied a punctuation just loop and ignore the following spaces until the first non-space char.

类似地,一旦您复制了一个标点符号循环并忽略以下空格,直到第一个非空格字符。

I could have written code. It would have been shorter. But i prefer letting you finish your homework with full understanding of the approach.

我可以写代码。它会更短。但是我更愿意让你在充分理解方法的情况下完成作业。