How to use regular expressions in Java

Asked By 0 points N/A Posted on -
qa-featured

Hi,

I am developing applications in Java and i use Eclipse platform to built applications in Java.

i created a form, registration form for my application and i want to apply form validation technique on that registration form. While I developing application in C# I use Regular Expressions and I don’t know how to do this in Java.

My registration Form is shown below.

Can anyone help me to solve this problem.

 

 

SHARE
Answered By 0 points N/A #117895

How to use regular expressions in Java

qa-featured

Java has support in regular expression through java.util.regex package. And you need to compile your regular expression through Pattern.compile () class factory. There is also a Matcher class that interprets the pattern and execute match operations.

Sample code:

import java.util.regex.Matcher;

import java.util.regex.Pattern;

public class RegexMatcher

{

    public static void main (String args[])

    {

         String email_ad = “[email protected]”;

         String reg_ex = “@{1}”; \ look for a @ symbol in email_ad and only one

         // create pattern object to compile regex

         Pattern pattern = Pattern.compile(reg_ex);

          // create matcher object to perform matching on the pattern set

       Matcher matcher = pattern.matcher(email_ad);

         if (matcher.find())

       {

            System.out.println(“Email ad verified.”);

        }

    }

}

Related Questions