---
title: "How to use regular expression in Java "
author: "love~ruby+rails"
date: "2009-09-22T09:06:17Z"
tags: []
slug: "how-to-use-regular-expression-in-java"
draft: false
lastmod: "2009-09-22T09:06:17Z"
id: 473931
url: "https://cursed.iteye.com/blog/473931"
---

This tip shows the way to validate a string with respect to a regular expression. java.util.regex package is used for regular expression related operations.

In this example the input string (inputStr) is validated with the regular expression (rgString).

&lt;!-- start source code --&gt;

`import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegularExpExp {          public static void main(String[] args) {                  String rgString = "write your regulat expression here";         CharSequence inputStr = "write your input string that need to be validated";         Pattern pattern = Pattern.compile(rgString);         Matcher matcher = null;         matcher = pattern.matcher(inputStr);                  if (matcher.matches()) {             System.out.println("Matched");         } else {             System.out.println("Not Matched");         }     } }`