开发者

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.

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. 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.


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)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜