-1
我有一种情况,其中相同的模式适用于Java,不适用于Apache常规experssions API。正则表达式与Apache正则表达式
import java.util.regex.*;
import org.apache.oro.text.regex.MalformedPatternException;
import org.apache.oro.text.regex.PatternCompiler;
import org.apache.oro.text.regex.PatternMatcher;
import org.apache.oro.text.regex.Perl5Compiler;
import org.apache.oro.text.regex.Perl5Matcher;
public class PatternExample {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("^(?=.*?[a-zA-Z])(?=.*?[0-9])[a-zA-Z0-9\\p{Punct}]+"); //working
Matcher matcher = pattern.matcher("[email protected]");
System.out.println("Input String matches regex - "+matcher.matches());
match("[email protected]");
}
public static MatchResult match(String input) {
PatternCompiler compiler = new Perl5Compiler();
PatternMatcher matcher = new Perl5Matcher();
org.apache.oro.text.regex.Pattern pattern = null;
if (pattern == null) {
try {
pattern = compiler.compile("^(?=.*?[a-zA-Z])(?=.*?[0-9])[a-zA-Z0-9\\p{Punct}]+");
} catch (MalformedPatternException e) {
System.out.println(e.getMessage());
e.printStackTrace();
return null;
}
}
if (matcher.matches(input, pattern)) {
System.out.println("true");
}else{
System.out.println("false");
}
return null;
}
}
上面的代码是让输出的主要方法模式中两种不同的方式,同时匹配方法提供虚假工作。
基本上我的要求是允许字母数字和Java和JavaScript中的一些特殊字符我使用Perl5Compiler在JavaScript中进行验证,并且再次使用Java进行验证。
JavaScript验证 功能_validatePasswordCharacter(/ *部件* /场){
var regExp;
var regExpInput = "^(?=.*?[a-zA-Z])(?=.*?[0-9])[a-zA-Z0-9\\p{Punct}]+";
console.log(regExpInput);
try
{
regExp = new RegExp(regExpInput);
}
catch (Error)
{
console.debug('Error while parsing the password charset');
return false;
}
if (!regExp.test(field.get('value')))
{
console.debug('Password not matching regular expression check -- portal.properties');
return false;
}
else{
console.debug('Password matching regular expression check -- portal.properties');
return true;
}
}
请建议向前
的JavaScriptŧ这里是没有JavaScript在这里... – Cerbrus
我不知道{Punct}部分;尝试(只是作为一个测试)删除它,并明确说明你想要什么(在这种情况下:'[!'#$%&'()* +,。/ :; <=>?@ [\]^_'{| }“# – dquijada
[”有些人在遇到问题时想'我知道,我会用正则表达式',现在他们有两个问题。“](https://blog.codinghorror.com/正则表达式现在你有两个问题/ /)严重的是,你可以极大地简化事情*不*尝试在单个正则表达式中完成所有事情*全部在一行中执行并不意味着它会更快或更好。 – VGR