Java Regex Pattern to allow characters and spaces Example

Java Regex Pattern to allow characters and spaces Example:

Requirements:

 Java domain   - Should be allowed
               - Should Not allowed
     Java      - Should be allowed
  (Java)       - Should Not allowed
Javadomain     - Should be allowed

Java Regex Program to allow characters and whitespaces And also not allow only whitespaces:

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

public class RegexSpace {

	public static void main(String[] args) {
		String input = " Java domain  ";
		Pattern regex = Pattern
				.compile("^$|[~\\!\\@\\#\\$\\%\\^\\&\\.\\,\\*\\(\\)\\_\\+\\=\\{\\}\\|\\:\\\\'\\[\\]\\<\\>\\?\\/\\'\\^[0-9]]");
		Matcher matcher = regex.matcher(input.trim());
		if (matcher.find()) {
			System.out.println("Not Allowed");
		} else {
			System.out.println("Allowed");
		}
	}

}

Output:

Allowed

Recommended Java Books:

270 total views, 2 views today

Leave a Reply

Your email address will not be published. Required fields are marked *