Author: techfox9

Java.util.regex Example

Wednesday, June 27th, 2012 @ 4:27 pm

import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class RegExp1 {
    private static final Pattern snRegExpPattern = Pattern.compile("^[A-Za-z0-9-_]+");

    public static void main(String []args) {
        checkMatch( "ab.de" );

        checkMatch( "abde" );

        checkMatch( "gh+ij" );

        checkMatch( "kl_mn" );

        checkMatch( "op-qr" );

        checkMatch( "st93uv" );

        checkMatch( "st93uv&" );

    }

    private static void checkMatch(String name) {
        Matcher reMatcher = snRegExpPattern.matcher(name);
        if (! reMatcher.matches() ) {
            System.out.println("Error on " + name);
        }
        else {
            System.out.println("OK on " + name);
        }
    }
}

Expected output :

Error on ab.de
OK on abde
Error on gh+ij
OK on kl_mn
OK on op-qr
OK on st93uv
Error on st93uv&

Java, JavaUsage


 


Comments are closed.