简体   繁体   English

在Java 1.4中使用正则表达式

[英]Using regex in java 1.4

I am trying to write regex to valid a string which should contain "A - Z", "a - z", "0 - 9", "-" , "_", "/" , "." 我正在尝试将正则表达式写为有效的字符串,该字符串应包含“ A-Z”,“ a-z”,“ 0-9”,“-”,“ _”,“ /”,“”。 , "%" , and ":"(period) characters only. ,“%”和“:”(句点)字符。

I have written code as below: 我写了如下代码:

import org.apache.regexp.RE;

 private static boolean checkInvalidCharacters (String identity){
        RE re = new RE("(\\w%-/:.)");
   if (!re.match (identity)){
           return true;         
       }
       return false;
   }

I am not sure why this does not work? 我不确定为什么这行不通?

Apart from letters and digits special characters required are - / . 除字母和数字外,所需的特殊字符是-/。 % : _ %:_

Your regex should be: 您的正则表达式应为:

[-\\w/.%:]+

Instead of using org.apache.regexp.RE you can use Java String API to validate: 代替使用org.apache.regexp.RE ,可以使用Java String API进行验证:

private static boolean checkInvalidCharacters (String identity) {
   return !identity.matches("[-\\w/.%:]+");
}

这对我有用:

"^[\\w/.%:\\-]+$"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM