验证字符串与正则表达式的机器人 [英] validate string with regex on android

查看:349
本文介绍了验证字符串与正则表达式的机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想,我的字符串不包含 * ; $ 。我用这个code

I want that my string didn't contain *,; and $. I use this code

private static boolean IsMatch(String s, String pattern) {
         try {
             Pattern patt = Pattern.compile(pattern);
             Matcher matcher = patt.matcher(s);
             return matcher.matches();
         } catch (RuntimeException e) {
           return false;
         }  
}



String regex ="[^*;$]";
System.out.println(IsMatch(url,regex));

但这种方法返回始终为false。 任何人能告诉我出了什么问题

But this method return always false. Can any one tell me what's the problem

推荐答案

尝试使用 [^ *; $] * 为您的正则表达式。这将返回true,如果您的字符串不包含任何的 * ; $ 。我假设你想,因为你已经在使用内部<$ ^ 的正则表达式匹配的字符串不的包含任何这些字符C $ C> [和]

Try using [^*;$]* for your regex. It'll return true if your string doesn't contain any of *, ; and $. I'm assuming you want your regex to match strings that don't contain any of those characters since you're already using ^ inside [ and ].

这篇关于验证字符串与正则表达式的机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆