【问题标题】:how to find the first word in the sentence having 'w' in it如何找到句子中包含“w”的第一个单词
【发布时间】:2011-01-27 08:25:41
【问题描述】:

我怎样才能找到我的句子中的第一个单词有 'w' 字符。这个字符可以出现在我的单词中的任何位置。句子示例“Hi xyzwy!你在这里做什么?”所以结果应该是“xyzwy”。

【问题讨论】:

    标签: c++ text-search


    【解决方案1】:

    从第一个字符到最后一个字符遍历你的字符串。检查是否遇到'w'。如果是,则回溯,直到遇到单词分隔符(例如空格)或到达字符串的开头,然后打印所有字符,直到遇到另一个单词分隔符(或字符串的结尾)。

    string Str;
    getline(cin, Str);
    
    for ( int i = 0; i < Str.length(); ++i )
      if ( Str[i] == 'w' )
      {
        // backtrack and print
        break;
      }
    

    或者使用string类的find method为你做搜索,那么你只需要识别单词。

    【讨论】:

      【解决方案2】:
      boost::optional<std::string>
      find_word_with(std::string const& haystack, std::string const& needle) {
        std::istringstream ss (haystack);
        for (std::string word; ss >> word;) {
          if (word.find(needle) != word.npos) {
            return boost::optional<std::string>(word);
          }
        }
        return boost::optional<std::string>();
      }
      
      std::string const whitespace = " \t\r\n\v\f";
      boost::optional<std::string>
      find_word_with2(std::string const& haystack, std::string const& needle) {
        typedef std::string::size_type Pos;
      
        Pos found = haystack.find(needle);
        if (found == haystack.npos) {
          return boost::optional<std::string>();
        }
      
        Pos start = haystack.find_last_of(whitespace, found);
        if (start == haystack.npos) start = 0;
        else ++start;
      
        Pos end = haystack.find_first_of(whitespace, found+1);
        if (end == haystack.npos) end = haystack.length();
      
        return boost::optional<std::string>(haystack.substr(start, end - start));
      }
      

      这两个只是空格上的单独单词(我错过了您最初想要“xyzwy”而不是“xyzwy!”),但您可以修改它们以忽略标点符号。第一个不太适合,但第二个可以很容易地修改为使用 find_first/last_not_of 与正则表达式 \w ("ABC. .abc..012.._") 而不是检查空格。

      请注意,第二个使用硬编码的 whitespace 变量,不像流解决方案(使用最后设置的全局语言环境)那样支持区域设置,但它可能正是您想要的。

      int main() {
        {
          boost::optional<std::string> r =
            find_word_with("Hi xyzwy! what are you doing here?", "w");
          if (!r) std::cout << "not found\n";
          else std::cout << "found: " << *r << '\n';
        }
        {
          boost::optional<std::string> r =
            find_word_with2("Hi xyzwy! what are you doing here?", "w");
          if (!r) std::cout << "not found\n";
          else std::cout << "found: " << *r << '\n';
        }
        return 0;
      }
      

      【讨论】:

        【解决方案3】:

        如果你真的需要正则表达式,你可以使用

        \w*w\w*
        

        例如:

        #include <boost/regex.hpp>
        #include <string>
        #include <iostream>
        using namespace boost;
        using namespace std;
        
        int main () {
            string s;
            getline(cin, s);
            match_results<string::const_iterator> m;
            if (regex_search(s, m, regex("\\w*w\\w*"))) {
                cout << "Found: " << string(m[0].first, m[0].second) << "\n";
            } else {
                cout << "Not found\n";
            }
            return 0;
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-04-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-07-16
          • 2011-07-20
          • 1970-01-01
          相关资源
          最近更新 更多