我有一个表单(JTable)用于请求防火墙访问。对于用户输入源的JTextField,我想在用户输入时实时验证输入。有效输入将以绿色突出显示。不应该被允许无效。
以下正则表达式是有效输入:
private static final String IP_Address = "\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\d";//56.1.2.3
private static final String IP_WithMask = "(\\d{1,3}.){3}(\\d{1,3})(?:\\s+[255])(\\d{1,3}.){3}(\\d{1,3})"; //56.1.2.3 255.255.255.254
private static final String IP_CIDR = "(\\d{1,3}.){3}(\\d{1,3})(?:\\s*/)(\\d{1,3})"; //56.1.2.3/24
private static final String IP_ADDRESS_Dash_Numeric_RANGE = "((\\d{1,3}.){3}(\\d{1,3})(?:\\s*-)(\\d{1,3}))";// 56.1.2.3-4
private static final String IP_ADDRESS_Dash_ADDRESS_RANGE = "((\\d{1,3}.){3}(\\d{1,3})(?:\\s*-\\s*)(\\d{1,3}.){3}(\\d{1,3}))";//56.1.2.3-56.1.2.5
private static final String IP_ADDRESS_To_Numeric_RANGE = "(\\d{1,3}.){3}(\\d{1,3})(?:\\s*[T|t][O|o]\\s*)(\\d{1,3})";//56.1.2.3 to 255
private static final String IP_ADDRESS_To_ADDRESS_RANGE = "((\\d{1,3}.){3}(\\d{1,3})(?:\\s*[T|t][O|o]\\s*)(\\d{1,3}.){3}(\\d{1,3}))";//56.1.2.3 to 56.1.3.5
在JTextField中,用户可以键入这些的任何排列。我需要识别/验证每种类型以便以后采取行动。
我正在使用
public class MyVerifyer extends InputVerifier{}
并将其应用于我的JTextfield:
setInputVerifier();
问题: 1.有人可以给我一个能够捕获所有这些案例的正则表达式吗? 2.当你打字的时候,你如何实时匹配。也许将TextListener附加到JTextField?
如有必要,我可以提供更多代码。
答案 0 :(得分:0)
我认为您需要尝试这样的事情(未经测试):
private static final String IP_Address = "\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\d";//56.1.2.3
private static final String IP_WithMask = "(\\d{1,3}.){3}(\\d{1,3})(?:\\s+[255])(\\d{1,3}.){3}(\\d{1,3})"; //56.1.2.3 255.255.255.254
private static final String IP_CIDR = "(\\d{1,3}.){3}(\\d{1,3})(?:\\s*/)(\\d{1,3})"; //56.1.2.3/24
private static final String IP_ADDRESS_Dash_Numeric_RANGE = "((\\d{1,3}.){3}(\\d{1,3})(?:\\s*-)(\\d{1,3}))";// 56.1.2.3-4
private static final String IP_ADDRESS_Dash_ADDRESS_RANGE = "((\\d{1,3}.){3}(\\d{1,3})(?:\\s*-\\s*)(\\d{1,3}.){3}(\\d{1,3}))";//56.1.2.3-56.1.2.5
private static final String IP_ADDRESS_To_Numeric_RANGE = "(\\d{1,3}.){3}(\\d{1,3})(?:\\s*[T|t][O|o]\\s*)(\\d{1,3})";//56.1.2.3 to 255
private static final String IP_ADDRESS_To_ADDRESS_RANGE = "((\\d{1,3}.){3}(\\d{1,3})(?:\\s*[T|t][O|o]\\s*)(\\d{1,3}.){3}(\\d{1,3}))";//56.1.2.3 to 56.1.3.5
private static String regex_array = IP_Address + "|" + IP_WithMask + "|" + IP_CIDR + "|" + IP_ADDRESS_Dash_Numeric_RANGE + "|" + IP_ADDRESS_Dash_ADDRESS_RANGE + "|" + IP_ADDRESS_To_Numeric_RANGE + "|" + IP_ADDRESS_To_ADDRESS_RANGE;
public class MyInputVerifier extends InputVerifier {
@Override
public boolean verify(JComponent input) {
String text = ((JTextField) input).getText().trim();
if (text.isEmpty()) return false;
if (text.matches(regex_array)) return false;
return true;
}
答案 1 :(得分:0)
为了达到我想要的结果,我使用了DocumentListener并将其分配给我的TextField。利用insertUpdate方法,我能够针对正则表达式实时验证输入。
@Override
public void insertUpdate(DocumentEvent e) {
// System.out.println("insert:" + field.getText());
m = p.matcher(this.field.getText());
while (m.find()) {
try {
String b = m.group();
System.out.println(b);
int index = field.getText().indexOf(b);
System.out.println("index=" + index);
int end = index + b.length();
System.out.println("end=" + end);
Highlighter.HighlightPainter painter = new DefaultHighlighter.DefaultHighlightPainter(Valid_COLOR);
this.field.getHighlighter().addHighlight(index, end, painter);
System.out.println(">> " + b.substring(2, b.length() - 2));
} catch (BadLocationException ex) {
System.out.println(ex);
}
}
}