[英]How to check if a string contains all of these: digits, alphabets and special characters?
[英]Efficient way to get all substrings from a string that contains special characters
我有这样的字符串:
std::string input = "This* #is # #just# a *random ##string #*that#
may contain any# char*cters#";
我需要获取所有的子字符串:
1)在字符“#”之间
和
2)包含字符“ *”
结果将是:
" a *random "
"*that"
" char*cters"
我这样做是这样的:
std::vector<std::string> substrings;
boost::split(substrings, input, boost::is_any_of("#"));
for (int i = 0; i < substrings.size(); i++)
{
if (i != 0 // first and last substring is not between '#' (only from one side)
&& (i != substrings.size() - 1)
&& !substrings[i].empty()
&& substrings[i].find('*') != std::string::npos) // if contain '*' character
{
// Here I've got my result
}
}
它可以工作,但是有什么有效的解决方案吗?
您可以使用正则表达式 "#([^#*]*[*][^#]*)#"
提取所有此类字符串。
该表达式描述了您要查找的子字符串的类型:
#
开始 #
以外的字符 #
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.