regular expression java text field(建立在正则表达式基础上的可以应用任何输入限制的text field)

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}$";
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值