[英]insert string that match regular expression
My program takes a string input from a user. 我的程序从用户获取字符串输入。 If the input string matches the regular expression then it should be inserted into the arrayList.
如果输入字符串与正则表达式匹配,则应将其插入到arrayList中。
I wrote that following. 我写了以下内容。 But, it does not work:
但是,它不起作用:
if( element.matches("[a-zA-Z]"));
{
set.add(element);
}
If that's actually how your code is written then the element
will always be added to the set
. 如果这实际上是代码的编写方式,则
element
将始终添加到set
。 You need to remove the ;
你需要删除
;
at the end of the first line for the conditional to work: 在条件工作的第一行的末尾:
if (element.matches("[a-zA-Z]")) {
set.add(element);
}
If you're trying to match more than more character you likely want "[a-zA-Z]+"
for the expression. 如果你想要匹配更多的角色,你可能想要
"[a-zA-Z]+"
表达式。
I guess you forgot the +
sign, meaning "once or more": 我想你忘记了
+
号,意思是“一次或多次”:
if (element.matches("[a-zA-Z]+")) {..}
(and of course, as the example above shows, you have to get rid of the semicolon) (当然,正如上面的例子所示,你必须摆脱分号)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.