How To Find Wild Cards In A String Using Regular Expressions?

This is how it began – our users started using wild cards (%%) sign in the search query. Since our legacy application had queries with like clause (apparently the original developers thought the end users are stupid jerks and would only key in the names). But there were smart users who knew about the wild card searches.

Our team came to a spotlight when a user used double wild cards in one of the search engines and brought the systems down !! Yes, it fetched millions of rows and the computations on those rows on a report led to JVM’s memory being depleted and the system crashed.

So how would you solve it instantly (I know it was just patching rather than redesign – but was important to bring the system up and was quick and cost effective solution). All we had to do was to see if the search fields had any wild cards in them. If they did, the search would not be performed and the user would be taken back to the search screen with some sort of errors.

In the following code, I have used the regular expressions to catch wild cards and the ones that enclose some text in between (alphanumeric and spaces to be exact). But you can always add more characters if you want.

package com.kushal.tools;
/**
 * @author Kushal Paudyal
 * WWW.icodejava.com
 * www.sanjaal.com/java
 * Finds if any wild cards (%%) are used in an String.
 * Wildcards can enclose digits or numbers or their combinations
 */
import java.util.regex.Pattern;

public class WildCardUsageFinderRegex {

    /**
     * Regular Expression to match the Wild Card Usage (% %) 
     * It tests for two % flags separated by N number of spaces or characters A-Z or a-z or 0-9. 
     * Can be modified to include other characters
     */
    private static final String WILD_CARD_EXPRESSION = "^%([ A-Za-z0-9])*%$";

    /**
     * Testing the validation with some sample wild cards
     */
    public static void main(String[] args) {

        String[] wildCards = { "% %", "%%", "%  ", "%dc%", "%123%" };

        int index = 0;
        boolean isMatch = false;
        while (index < wildCards.length) {
            isMatch = isWildCardUsed(wildCards[index]);
            System.out.println("String " + wildCards[index] + " - " + (isMatch ? "Valid" : "Invalid"));
            index++;
        }

    }

    /**
     * This method returns true if the parameter string contains a valid wild card
     */
    public static boolean isWildCardUsed(String string) {

        return Pattern.matches(WILD_CARD_EXPRESSION, string);
    }

}

If you run the program, you will find the following results.

String % % - Valid
String %% - Valid
String %   - Invalid
String %dc% - Valid
String %123% - Valid


You can use a simple solution like this to see if any fields have used wild cards.

How to validate date in Java using Regular Expressions

The following java program has the capability of doing the date format validation in software using regular expressions and is able to validate both mmddyyyy and ddmmyyyy date formats (of course the date, month and year can be separated using punctuations or delimiters).

package com.kushal.tools;
/**
 * @author Kushal Paudyal
 * Last Modified on 2010/12/21
 * 
 * This class demonstrates the use of regular expressions to validate Dates
 * It basically validates two formats of dates:MMDDYYYY & DDMMYYYY
 * Both of these validations accept -, / and . as delimiters.
 * 
 * www.sanjaal.com/java
 */

import java.util.regex.Pattern;

public class DateValidatorRegex {
	
	static String regexMMDDYYYY="^(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\\d\\d$";
	static String regexDDMMYYYY="^(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\\d\\d$";
	
	public static void main (String [] args ) {
		/**Testing for MMDDYYYY**/
		System.out.println("TEST FOR MMDDYYYY");
		System.out.println(isAValidMMDDYYYYDate("07-20-1982"));//true
		System.out.println(isAValidMMDDYYYYDate("07/20/1982"));//true
		System.out.println(isAValidMMDDYYYYDate("07.20.1982"));//true
		System.out.println(isAValidMMDDYYYYDate("07201982"));//false
		System.out.println(isAValidMMDDYYYYDate("20.07.1982"));//false
		
		/**Testing for MMDDYYYY**/
		System.out.println("\nTEST FOR DDMMYYYY");
		System.out.println(isAValidDDMMYYYYDate("20-07-1982"));//true
		System.out.println(isAValidDDMMYYYYDate("20/07/1982"));//true
		System.out.println(isAValidDDMMYYYYDate("20.07.1982"));//true
		System.out.println(isAValidDDMMYYYYDate("20071982"));//false
		System.out.println(isAValidDDMMYYYYDate("07.20.1982"));//false
		
	}
	/**
	 * @param date
	 * MMDDYYYY DATE VALIDATION
	 * It can validate the following three kinds of dates:
	 * 07-20-1982
	 * 07/20/1982
	 * 07.20.2982
	 */
	public static boolean isAValidMMDDYYYYDate(String date) {
		return Pattern.matches(regexMMDDYYYY, date);
	}
	
	/**
	 * @param date
	 * DDMMYYYY DATE VALIDATION
	 * It can validate the following three formats of dates:
	 * 20-07-1982
	 * 20/07/1982
	 * 20.07.1982
	 */
	public static boolean isAValidDDMMYYYYDate(String date)	{
		return Pattern.matches(regexDDMMYYYY, date);
	}
}

How to validate PO Box Address Variations With Java Regular Expressions?

Address validation in Java could be a little tricky thing if you are unwilling to use Java Regular Expression. This is because of complexity caused by the variations of the components of the address that people can use.

In this tutorial, we will focus on doing the PO Box validation in an address using regular expressions in Java. The tutorial contains test cases for variations of the PO Box formats.

package com.kushal.tools;
/**
 * @author Kushal Pauduyal
 * Created on: 05/11/2011
 * Last Modified On: 05/11/2011
 * 
 * This class contains method and unit tests to validate the PO Box in an address.
 * The regular expression can validate several different variations of PO Boxes.
 * This should pretty much satisfy all international PO Box numbers, if not the 
 * pattern can be modified to satisfy one.
 */
import java.util.regex.Pattern;

public class RegexPOBoxValidation {

	/**
	 * Patterns are greatest things in Java. The following pattern can identify the Post Box
	 */
	static final String thePattern = 
"([\w\s*\W]*(P(OST)?.?\s*((O(FF(ICE)?)?)?.?\s*(B(IN|OX|.?))|B(IN|OX))+))[\w\s*\W]*";	
	
	/**
	 * 
	 * @param str - PO Box string to validate
	 * @return if the input string is a valid PO Box format.
	 */
	public static boolean isValid(String str) {
		return Pattern.matches(thePattern, str);
	}

	public static void main (String [] args ) {
		/**
		 * Doing unit test with several different PO Box usage alterations.
		 */
		String [] itemsToValidate = {
				"PO Box",
				"P O Box",
				"P. O. Box",
				"P.O.Box",
				"Post Box",
				"Post Office Box",
				"Post Office",
				"P.O.B",
				"P.O.B.",
				"POB",
				"Post Office Bin",
				"Box",
				"Bin",
				"Post",
				"Postal Code",
				"100,, P O Box Des Moines",
				" P O Box DesMoines1000",
				" P O Box Des Moines 1000",
				" Post Office Box",
				" Post Office Box  ",
				"Post Box #"};										 

		for (int index = 0; index < itemsToValidate.length; index++) {
			String item = itemsToValidate[index];
			boolean isValid = isValid(itemsToValidate[index].toUpperCase());
			System.out.println(item + " : " + (isValid ? "Valid" : "Invalid"));
		}
	}
			
}


Here is the output of this java program:

PO Box : Valid
P O Box : Valid
P. O. Box : Valid
P.O.Box : Valid
Post Box : Valid
Post Office Box : Valid
Post Office : Invalid
P.O.B : Valid
P.O.B. : Valid
POB : Valid
Post Office Bin : Valid
Box : Invalid
Bin : Invalid
Post : Invalid
Postal Code : Invalid
100,, P O Box Des Moines : Valid
 P O Box DesMoines1000 : Valid
 P O Box Des Moines 1000 : Valid
 Post Office Box : Valid
 Post Office Box   : Valid
Post Box # : Valid