提取文本的一部分 [英] Extracting part of a text
问题描述
大家好,
我想提取从列表框中得到的部分文本.
这是场景:
Hi guys,
I want to extract part of a text I get from list box.
Here is the scenario:
int indexValue = m_listItem.GetCursel();
CString strText;
m_listItem.GetText(indexValue, strText);
我只是说出来,这样您才能了解其中的来源.
好吧,假设strText中的文本为:"1 17:28:07 76 172.17.70.10 62996 193.65.41.23 53"
我的问题是如何从文本中仅提取17:28:07或172.17.70.10.
非常感谢.
I only put that out so you can understand where am coming from.
Alright, assuming the text in the strText is: "1 17:28:07 76 172.17.70.10 62996 193.65.41.23 53"
My problem is how could I extract just 17:28:07 or 172.17.70.10 from the text.
Many thanks.
推荐答案
您可以使用正则表达式进行提取.也许只是将字符串拆分为一个空格字符,然后抓住相关部分就足够了.这实际上取决于输入的结构.
干杯
曼弗雷德(Manfred)
You could use regular expressions to do the extraction. Maybe just splitting the string at a space character and grabbing the relevant parts would suffice. That really depends on how your input is structured.
Cheers
Manfred
您可以(从最高级别向下...):
- (已经建议)使用模式匹配库,例如正则表达式解析器(谷歌提供免费的
C++
库).- 使用简单的
CString
方法,例如标记化 [
You may (from highest level downwards...):
- (As already suggest) Use a pattern matching library, like a regular expression parser (google for a free
C++
library).- Use simple
CString
methods like Tokenize[^].- Parse yourself the string for such patterns (you would have some fun with this approach...).
CString::Tokenize()
需要进行迭代,直到无法提取输入中的所有部分.
在标准C ++库的帮助下,您可以立即执行以下操作:
Hi,
CString::Tokenize()
requires to be iterated until failure to extract all the parts in your input.
With the help of the Standard C++ Library you can do it at once:
#include <vector>
#include <sstream>
#include <iterator>
#include <algorithm>
// ...
int indexValue = m_listItem.GetCursel();
CString strText;
m_listItem.GetText(indexValue, strText);
std::vector<std::string> parts;
std::istringstream iss(strText.GetBuffer());
std::copy(std::istream_iterator<std::string>(iss), std::istream_iterator<std::string>(), std::back_inserter(parts));
在"1 17:28:07 76 172.17.70.10 62996 193.65.41.23 53"
中strText
,您进入parts[0x00000007] ("1","17:28:07","76","172.17.70.10","62996","193.65.41.23","53")
.
欢呼声,
AR
With "1 17:28:07 76 172.17.70.10 62996 193.65.41.23 53"
in strText
, you get in parts[0x00000007] ("1","17:28:07","76","172.17.70.10","62996","193.65.41.23","53")
.
cheers,
AR
这篇关于提取文本的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!