在句點和逗號之前從字符串中刪除空格

[英]Remove spaces from string before period and comma


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.

我可以寫代碼。它會更短。但是我更願意讓你在充分理解方法的情況下完成作業。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2016/12/02/2fa82dbd38605028681903a03bdc9fb3.html



 
  © 2014-2022 ITdaan.com 联系我们: