我有关于手机的推文列表和手机名称列表,现在我必须对每个手机名称进行计数。我使用数组列表来获取手机的名称,如下所示
brand_list.add("Samsung Galaxy S5");
brand_list.add("Nolia Lumia 525");
然后我收到了有关手机的推文列表,例如“ RT @protectyrbubble:#PYBS5giveaway #WIN赢得三星Galaxy S5。只需遵循@protectyrbubble和RT!详细信息和条款和条件http://t.co/u0NTM00rhA ht…”
然后我使用以下代码对每个电话进行计数,如下所示
for(int j=0;j<array_list.size();j++)
{
pattern = Pattern.compile(" ((.*)Samsung(.*)Galaxy(.*)S5(.*)",Pattern.CASE_INSENSITIVE) ;
matcher = pattern.matcher(array_list.get(j).toString());
while (matcher.find())
{
count++;
}
}
在上面,array_list保存关于手机的推文。现在如果我使用上面的正则表达式,它对于上面提到的推文工作正常,但不适用于像这样的字符串
“ Galaxy S5 Mini已出现在三星网站http://t.co/sinWiLpUNV上”
因此,我需要一个正则表达式也可以找到上述推文。
提前致谢
您不能使用正则表达式检查订单。但似乎,如果你只是想知道,如果字符串"Samsung"
,"Galaxy"
并"S5"
包含在字符串,所以你可以只问了3场比赛:".*Samsung.*"
,".*Galaxy.*"
和".*S5.*"
。
该String#contains()
方法也是可行的,但是不幸的是,它不能不区分大小写地进行检查。
编辑:它可能与类似的东西一起工作,"(.*(Samsung|Galaxy|S5))*.*"
但是我不确定正确的语法...也许你明白我的想法。
如果您的电话名称在内brand_list
,则可以执行以下操作:
for(int j=0;j<array_list.size();j++)
{
boolean allIn = true;
for (String phoneName: brand_list)
{
String[] phoneWords = phoneName.split(" ");
for (int wordIndex = 0; wordIndex < phoneWords.length; wordIndex++)
{
String regexPattern = "(.*)" + phoneWords[wordIndex] + "(.*)";
pattern = Pattern.compile(regexPattern, Pattern.CASE_INSENSITIVE);
matcher = pattern.matcher(array_list.get(j).toString());
if (!matcher.find())
{
allIn = false;
}
}
}
System.out.println(allIn); // should be false here if one of the words
// couldn't be found in the strings and
// should be true otherwise
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句