Monday, May 04, 2009

Fun soft: regex checker

I'm not a regular expressions expert but I use them often - as probably most of software developers. Before using them I want to be sure my regexes are written correctly and they will serve their purposes. I wrote very small command-line app for doing the check for me :) As a first input user provides the regex to check, then the user can type all texts to check against previously given regex until she types 'q' or 'exit' (for which program exits). Enjoy!

package com.bielu;


public class RegexTester {
public static void main(String[] args) {
Console in = System.console();
if (in == null) {
System.err.println("Cannot obtain system console - exiting application");

System.out.print("Enter regex to test: ");
String regex = in.readLine();

while (true) {
System.out.printf("Type value to match with given regex '%s': ", regex);
String string = in.readLine();

if (string.equalsIgnoreCase("q") || string.equalsIgnoreCase("exit")) {

if (string.matches(regex)) {
System.out.printf("'%s' matches '%s' regex\n", string, regex);
} else {
System.out.printf("'%s' does NOT match '%s' regex\n", string, regex);

No comments: