如何找到句子中包含“w”的第一个单词在其中

发布于 2024-08-25 14:29:46 字数 86 浏览 1 评论 0原文

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

how can i find the first word in my sentence having 'w' character.This character can be present anywhere in my word.example of sentence "Hi xyzwy! what are you doing here?" So the result should be "xyzwy".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蒗幽 2024-09-01 14:29:46

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

string Str;
getline(cin, Str);

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

或者使用 string 类的 find 方法 来为您进行搜索,那么你只需要识别这个词。

Go over your string starting from the first character until the last. Check if you encounter 'w'. If yes, backtrack until you hit a word separator (a space for example) or you reach the beginning of your string, then print all characters until you encounter another word separator (or the end of the string).

string Str;
getline(cin, Str);

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

Or use the find method of the string class to do the searching for you, then you just need to identify the word.

浅浅淡淡 2024-09-01 14:29:46

如果你确实需要正则表达式,你可以使用

\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;
}

If you really need regex, you can use

\w*w\w*

For example:

#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;
}
抚你发端 2024-09-01 14:29:46
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 等效的内容code> ("ABC..abc..012.._") 而不是检查空格。

请注意,第二个使用硬编码的空白变量,不像流解决方案(使用最后设置的全局区域设置)那样具有区域设置感知能力,但它可能正是您想要的。

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;
}
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));
}

Both of these only separate words on whitespace (I missed that you wanted "xyzwy" instead of "xyzwy!" at first), but you could modify them to ignore punctuation. The first isn't very amenable to that, but the second could be easily modified to use find_first/last_not_of with the equivalent of regex \w ("ABC..abc..012.._") instead of checking for whitespace.

Note that the second, using the hardcoded whitespace variable, is not locale-aware as the stream solution is (which uses the last set global locale), but it may be just what you want.

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文