本文介绍了如何使用正则表达式从 CString 中提取名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
限时送ChatGPT账号..采用具有以下定义的 CString
:
Take a CString
that has the following definition:
CString strNameText = _T("Mr Happy [12]");
我有一系列的名字,所有的名字都有空格,最后是括号.现在我只想从字符串中提取名称.
I have a series of names with all have the space with a number is brackets at the end. Now I want to extract just the name from the string.
我意识到我可以找到"的第一个索引["
然后提取左侧的文本直到该索引.但是我可以使用任何正则表达式命令来做同样的事情吗?
I realise that I can find the first index of " ["
and then extract the text on the left up to that index. But can I use any regex command to do the same?
推荐答案
我可能会建议在此处替换正则表达式,以删除字符串末尾的括号术语.
I might suggest a regex replacement here, to remove the bracketed term at the end of the string.
std::string strNameText ("Mr Happy [12]");
std::regex r ("\\s+\\[\\d+\\]$");
std::cout << std::regex_replace (strNameText, r, ""); // Mr Happy
C++ 示例
#include <regex>
CString strNameText = _T("Mr Happy [12]");
std::string s1 = CT2A(strNameText);
std::regex r("\\s+\\[\\d+\\]$");
std::string s2 = std::regex_replace(s1, r, "");
return CString(s2.c_str());
这篇关于如何使用正则表达式从 CString 中提取名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论