簡體   English   中英

使用正則表達式C ++進行搜索

[英]Searching using regular expressions C++

我正在使用Boost.Regex來實現這樣的目標:搜索“|” 然后取“|”的左邊部分 並把它放在一個字符串,與右邊部分相同:

string s1;
string s2;
who | sort

在此之后,s1應該是“誰”,而s2應該是“排序”。
如果我記得很好,它在Python中是可行的,我怎么能在Boost中使用正則表達式呢?

謝謝。

#include <boost/algorithm/string.hpp>
std::vector<std::string> strs;
boost::split(strs, "string to split", boost::is_any_of("|"));

用C ++拆分一個字符串?

這是簡短的樣本:

#include <iostream>
#include <boost/regex.hpp>

int main()
{
  // expression
  boost::regex exrp( "(.*)\\|(.*)" );
  boost::match_results<std::string::const_iterator> what;
  // input
  std::wstring input = "who | sort";
  // search
  if( regex_search( input,  what, exrp ) ) {
    // found
    std::string s1( what[1].first, what[1].second );
    std::string s2( what[2].first, what[2].second );
  }

  return 0;
}

另外,您可能希望查看Boost.Tokenizer

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM