'X'
假设一种通用方法,在掩码中的一个字符(例如)与正则表达式的一部分(例如 )之间存在映射'.'
,并且经常出现的掩码字符会产生一个数字量词(例如{3}
)。
所以我把一个辅助类和一个简单的测试方法放在一起,所以也许这是一个起点。
助手类:
import java.util.HashMap;
import java.util.Map;
public class PatternBuilder {
protected Map<Character, String> mappings = new HashMap<Character, String>();
protected boolean caseSensitive = false;
public PatternBuilder() {
}
public PatternBuilder(boolean caseSensitive) {
this.caseSensitive = caseSensitive;
}
public PatternBuilder addDefinition(char input, String mapping) {
if (this.caseSensitive) {
this.mappings.put(input, mapping);
} else {
this.mappings.put(Character.toLowerCase(input), mapping);
}
return this;
}
public String buildRegexPattern(String mask) {
if ((mask == null) || (mask.length() == 0)) {
return "";
}
StringBuilder patternBuffer = new StringBuilder();
char lastChar = 0;
int count = 0;
for (int i = 0; i < mask.length(); i++) {
char c = mask.charAt(i);
if (this.caseSensitive == false) {
c = Character.toLowerCase(c);
}
if (c != lastChar) {
if (count > 0) {
String mapped = mappings.get(lastChar);
if (mapped == null) {
// mapping for char not defined
return "";
}
patternBuffer.append(mapped);
patternBuffer.append("{").append(count).append("}");
}
lastChar = c;
count = 1;
} else {
count++;
}
}
if (count > 0) {
String mapped = mappings.get(lastChar);
if (mapped == null) {
mapped = ".";
}
patternBuffer.append(mapped);
patternBuffer.append("{").append(count).append("}");
}
return patternBuffer.toString();
}
}
用法:
PatternBuilder patternBuilder = new PatternBuilder()
.addDefinition('X', ".")
.addDefinition('Y', "Y")
.addDefinition('M', "[0-9]")
.addDefinition('N', "\\d")
.addDefinition('A', "[a-zA-Z]");
String rePattern = patternBuilder.buildRegexPattern("XxxYMXXXXXA"); // case insensitive, x == X
System.out.println("Pattern: '" + rePattern + "'");
Pattern p = Pattern.compile(rePattern);
String[] tests = new String[]{
"39JY412345O", // Original, match
"39JY41234FO", // replaced 5 with F, still matching
"39JY4123457", // replaced O with 7, no match
"A9JY4123457" // replaced 3 with A, no match
};
for (String s : tests) {
Matcher m = p.matcher(s);
System.out.println("Test '" + s + "': " + m.matches());
}
我的输出:
Pattern: '.{3}Y{1}[0-9]{1}.{5}[a-zA-Z]{1}'
Test '39JY412345O': true
Test '39JY41234FO': true
Test '39JY4123457': false
Test 'A9JY4123457': false