Regex的interface里有一些正则表达式的例子,有兴趣的朋友可以扩展它,譬如EMail,IP地址。
用法:new RegexTextField(Regex.regex_1to365) 就创建了一个只能输入1到365之间数字的text文本框
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;
/**
* Description: the regular expression document for RegexTextField
*/
public class RegexDocument extends PlainDocument {
private static final long serialVersionUID = -6472743317489015963L;
private String regex = null;
private Pattern pattern = null;
public RegexDocument() {
super();
}
public RegexDocument(String regex) {
super();
this.setRegex(regex);
}
public String getRegex() {
return regex;
}
public void setRegex(String regex) {
this.regex = regex;
this.pattern = Pattern.compile(regex);
}
public void insertString(int offs, String str, AttributeSet a)
throws BadLocationException {
String lastText = this.getText(0, this.getLength());
StringBuffer sb = new StringBuffer(lastText);
sb.insert(offs, str);
String curText = sb.toString();
if (pattern != null) {
Matcher matcher = pattern.matcher(curText);
if (matcher.matches())
super.insertString(offs, str, a);
}
}
}
import javax.swing.JTextField;
/**
* Description: RegexTextField can input any text with any limitation only with a regular
* expression
*/
public class RegexTextField extends JTextField {
private static final long serialVersionUID = -5407675386359166020L;
public RegexTextField() {
super();
}
public RegexTextField(String regex) {
super();
this.setDocument(new RegexDocument(regex));
}
public void setRegex(String regex) {
this.setDocument(new RegexDocument(regex));
}
}
/**
* A regular expression based implementation of <code>AbstractFormatter</code>.
*
* @author will.wang
* @version 1, 06/01/09
*/
public class RegexFormatter extends DefaultFormatter {
/**
*
*/
private static final long serialVersionUID = 1L;
private Pattern pattern;
private Matcher matcher;
public RegexFormatter() {
super();
}
/**
* Creates a regular expression based <code>AbstractFormatter</code>.
* <code>pattern</code> specifies the regular expression that will be used
* to determine if a value is legal.
*/
public RegexFormatter(String pattern) throws PatternSyntaxException {
this();
setPattern(Pattern.compile(pattern));
}
/**
* Creates a regular expression based <code>AbstractFormatter</code>.
* <code>pattern</code> specifies the regular expression that will be used
* to determine if a value is legal.
*/
public RegexFormatter(Pattern pattern) {
this();
setPattern(pattern);
}
/**
* Sets the pattern that will be used to determine if a value is legal.
*/
public void setPattern(Pattern pattern) {
this.pattern = pattern;
}
/**
* Returns the <code>Pattern</code> used to determine if a value is legal.
*/
public Pattern getPattern() {
return pattern;
}
/**
* Sets the <code>Matcher</code> used in the most recent test if a value
* is legal.
*/
protected void setMatcher(Matcher matcher) {
this.matcher = matcher;
}
/**
* Returns the <code>Matcher</code> from the most test.
*/
protected Matcher getMatcher() {
return matcher;
}
/**
* Parses <code>text</code> returning an arbitrary Object. Some formatters
* may return null.
* <p>
* If a <code>Pattern</code> has been specified and the text completely
* matches the regular expression this will invoke <code>setMatcher</code>.
*
* @throws ParseException
* if there is an error in the conversion
* @param text
* String to convert
* @return Object representation of text
*/
public Object stringToValue(String text) throws ParseException {
Pattern pattern = getPattern();
if (pattern != null) {
Matcher matcher = pattern.matcher(text);
if (matcher.matches()) {
setMatcher(matcher);
return super.stringToValue(text);
}
throw new ParseException("Pattern did not match", 0);
}
return text;
}
}
public interface Regex {
/**
* Same as new IntegerTextField(0,365) Acceptable value is from 1 to 365
*
*/
String regex_1_365 = "^[1-9]|[1-9][0-9]|[12][0-9]{2}|3[0-5][0-9]|36[0-5]$";
/**
* Same as new IntegerTextField(0,200000) Acceptable value is from 1 to
* 200000
*
*/
String regex_1_200000 = "^[1-9][0-9]{0,4}|1[0-9]{0,5}|20{5}$";
/**
* Same as new IntegerTextField(1,200000) Acceptable value is from 0 to
* 200000
*
*/
String regex_0_200000 = "^0|[1-9][0-9]{0,4}|1[0-9]{0,5}|20{5}$";
/**
* user password, 6-15 word character: [a-zA-Z_0-9]
*/
String regex_user_password = "^//w{6,15}$";
/**
* user password, 6-15 word character: [a-zA-Z_0-9], user can type from none
*/
String user_password = "^//w{0,15}$";
/**
* file name, any charactre
*/
String file_name = "^[^///:*?<>|]+$";
/**
* snmp oid, suppose start with .1.3.6.1.4.1
*/
String regex_snmp_oid = "^//.1//.3//.6//.1//.4//.1(//.//d+)*$";
/**
* IP address, 1-255.0-255.0-255.1-255
*/
String regex_ip_address = "^(//d{1,2}|1//d//d|2[0-4]//d|25[0-5])(.(//d{1,2}|1//d//d|2[0-4]//d|25[0-5])){3}$";
//let user can input from none with document of a text editor
String ip_address = "^(//d{1,2}|1//d//d|2[0-4]//d|25[0-5])?(//.|//.(//d{1,2}|1//d//d|2[0-4]//d|25[0-5])){0,3}$";
/**
* MAC address,
*/
// String regex_mac = "^([0-9A-F]{2})(-[0-9A-F]{2}){5}$";
String regex_mac = "^([0-9A-F]{2})?(-|-[0-9A-F]{2}){5}$";
}