Sign in to follow this  
Glass_Knife

[java] Java Regex IP Address and JTextField

Recommended Posts

Glass_Knife    8636
First of all, this is what I've come up with for having a JFormattedTextField validate an IP address. Someday, this may help someone, and since I went through the trouble of getting it working, I'm sharing the fun :) This is a RegexFormatter I found on the java website: Regular Expression Based AbstractFormatter
import java.text.ParseException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

import javax.swing.text.DefaultFormatter;

public class RegexFormatter extends DefaultFormatter {
    private Pattern pattern;
    private Matcher matcher;

    public RegexFormatter() {
        super();
    }

    public RegexFormatter(String pattern) throws PatternSyntaxException {
        this();
        setPattern(Pattern.compile(pattern));
    }

    public RegexFormatter(Pattern pattern) {
        this();
        setPattern(pattern);
    }

    public void setPattern(Pattern pattern) {
        this.pattern = pattern;
    }

    public Pattern getPattern() {
        return pattern;
    }

    protected void setMatcher(Matcher matcher) {
        this.matcher = matcher;
    }

    protected Matcher getMatcher() {
        return matcher;
    }

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



Here is an example of using the class with a regular expression...
String _255 = "(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)";
Pattern p = Pattern.compile( "^(?:" + _255 + "\\.){3}" + _255 + "$");
RegexFormatter ipFormatter = new RegexFormatter( p );
JFormattedTextField ipAddress = new JFormattedTextField( ipFormatter );

ipAddress.addPropertyChangeListener( "value",
    new PropertyChangeListener() {
        public void propertyChange( PropertyChangeEvent e ) {
           System.out.println( ipAddress.getValue() );
        }
    }
);



I was wondering if someone could explain to me how the expression works. I don't like not understanding stuff like this. I googled for regex tutorials, but they didn't help me much. String _255 = "(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)"; Pattern p = Pattern.compile( "^(?:" + _255 + "\\.){3}" + _255 + "$");

Share this post


Link to post
Share on other sites
Argus2    140
Well, the pattern of _255 is:
(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)

which is:
(?:
start non-capturing group (don't worry about this)
25[0-5]
match the digit 2, then the digit 5, then one of the digits 0,1,2,3,4, or 5
|
or
2[0-4][0-9]
match the digit 2; then 0,1,2,3,or 4; then 0,1,2,3,4,5,6,7,8,or 9 (any single digit really)
|
or
[01]?[0-9][0-9]?
match 0 or 1 or nothing; match any single digit; match any single digit or nothing
)
end non-capturing group

So then the full pattern is:
"^(?:" + _255 + "\\.){3}" + _255 + "$"
which is:
^
start of the input
(?:
start non-capturing group
String _255
the pattern of _255 must now match
\\.
match a period (backslash is escaped to show it is a real backslash, and the real backslash is needed as part of the pattern to escape the period, which otherwise matches any character.
)
end non-capturing group
{3}
match the group just ended 3 times
String _255
match 255 once more (note this time without the period at the end)
$
end of input


Basically it's a bad pattern to learn regular expressions from, because it has a few "advanced" features in it. Have a good look at the 'Pattern' class in the javadocs - the introduction is quite good.

Share this post


Link to post
Share on other sites
Glass_Knife    8636
Quote:
Original post by Argus2
Basically it's a bad pattern to learn regular expressions from, because it has a few "advanced" features in it. Have a good look at the 'Pattern' class in the javadocs - the introduction is quite good.


Thank you for explaining it.

Has anyone else tried the code? What do you think? Good, bad, to hard, not hard enough?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this