普通防爆pression的Andr​​oid [英] Regular Expression Android

查看:107
本文介绍了普通防爆pression的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查一个人品只能在Android设备上编辑视图存在。我是新来定期EX pression和我实现了即将举行的code。哪里是我错了?这里是我的code

  {尝试        模式模式= Pattern.compile(/ @);
        匹配匹配= pattern.matcher(valEmail);        如果(!matcher.matches()){            Toast.makeText(这一点,因维没有@字符,Toast.LENGTH_LONG).show();        }    }赶上(例外前){}


解决方案

有两个问题与您当前code:


  • 的斜线意味着它真​​的是在寻找/ @,而不仅仅是@。你似乎认为/做一个正则表达式中的一些特别的东西 - 它不

  • 您正在使用<一个href=\"http://docs.oracle.com/javase/7/docs/api/java/util/regex/Matcher.html#matches%28%29\"><$c$c>matches()它试图匹配的全部的输入的;你想<一个href=\"http://docs.oracle.com/javase/7/docs/api/java/util/regex/Matcher.html#find%28%29\"><$c$c>find这将只是试图找到一个匹配的正则表达式的地方的输入。

为什么要使用常规的前pressions在所有关系吗?为什么不干脆:

 如果(!valEmail.contains(@)){
    ...
}

这将检查地址是否包含的任何的@标志。如果您要检查,只有有有一个的@符号,你可以使用:

  INT atIndex = valEmail.indexOf('@');
如果(atIndex == -1){
    //处理为*否* @符号
}
如果(valEmail.indexOf('@',atIndex + 1)!= -1){
    //处理多个@迹象
}

如果您的的要使用常规的前pressions,有相当更复杂的电子邮件地址验证常规的前pressions可用。 (有很多不同的人有不同程度的有效性的 - 确保你得到一个专为正则表达式的Java的味道。)我不会使用一个的只是的这个,但 - 只使用常规的前pressions在那里你在模式匹配很感兴趣。

如果你想使用正则表达式为至少一个非@,@由以下,随后至少一个非@你可以使用:

  // TODO:一次和重用这个编译
模式模式= Pattern.compile([^ @] + @ ^​​ @] +);
匹配匹配= pattern.matcher(valEmail);如果(!matcher.matches()){
    ...
}

顺便说一句,这样的:

 赶上(例外前){}

是的从不的一个好主意。请不要忽略错误罔。

I am trying to check if an at character only exist in an android edit-view. I am new to regular expression and i implemented the forthcoming code. where am i mistaken ? Here is my code

try {

        Pattern pattern = Pattern.compile("/@");
        Matcher matcher = pattern.matcher(valEmail);

        if (!matcher.matches()) {

            Toast.makeText(this," Invid no @ character ",Toast.LENGTH_LONG).show();

        }

    }catch(Exception ex){}

解决方案

There are two problems with your current code:

  • The leading slash means it really is looking for "/@" rather than just "@". You seem to think that "/" does something special within a regex - it doesn't.
  • You're using matches() which tries to match the whole of the input; you want find which will just try to find a match for the regex somewhere in the input.

Why are you using regular expressions at all though? Why not just:

if (!valEmail.contains("@")) {
    ...
}

That will check whether the address contains any "@" signs. If you want to check that there's only one "@" sign, you could use:

int atIndex = valEmail.indexOf('@');
if (atIndex == -1) {
    // Handling for *no* @ sign
}
if (valEmail.indexOf('@', atIndex + 1) != -1) {
    // Handling for multiple @ signs
}

If you do want to use regular expressions, there are rather more sophisticated email address validation regular expressions available. (There lots of different ones with different levels of validity - make sure you get one designed for the Java flavour of regex.) I wouldn't use one just for this though - only use regular expressions where you're really interested in pattern matching.

If you want to use a regex for "at least one non-@, following by @, followed by at least one non-@" you could use:

// TODO: Compile this once and reuse
Pattern pattern = Pattern.compile("[^@]+@[^@]+");
Matcher matcher = pattern.matcher(valEmail);

if (!matcher.matches()) {
    ...
}

As an aside, this:

catch(Exception ex){}

is never a good idea. Please don't just ignore errors indiscriminately.

这篇关于普通防爆pression的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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