Regular Expressions in Java


A regular expression is a code that is used to match a pattern in a given string and is new to Java 1.4. Regular expressions are made up of normal characters and metacharacters. Normal characters are like letters, numbers, underscores, etc., whereas metacharacters are characters that have a special function and are used in conjunction with normal characters in order to define a type of pattern to match to string data. In the String class, you can use the method matches to match a regular expression passed as a parameter of type String to the characters in a String object, returning true if the match was found and false if it was not.

One of the simplest metacharacters is the full-stop (.), which is treated as any character when attempting to match a pattern. So let's say you had the regular expression "b.tter" and wanted to test this against a string.

String str1 = new String("better");
   String str2 = new String("butter");
   String regex = "b.tter";
   
   str1.matches(regex);     // returns true
   str2.matches(regex);     // returns true

In this case, matches on both string values will be found as the "." metacharacter simply matches the character at that index no matter what (for example, the string "bZtter" would match also).

You can use a regular expression to check if a string only contains alphabetical characters and spaces as follows:

String str1 = new String("Only letters and spaces");
   String str2 = new String("Other chars :@%#5365");
   String regex = "[A-Za-z ]{1,}";
   
   str1.matches(regex);     // returns true
   str2.matches(regex);     // returns false

The square brackets ([]) indicate that you want to match one of the characters specified between them. The A-Za-z means that the character can be any of the characters from A to Z or a to z, hence ignoring the case. Notice that there is a space after the lowercase z, which actually indicates that a space is included as one of the possible characters to match also. The {1,} code indicates that you want to match one or more instances of any of the characters between the square brackets. Thus, this regular expression finds matches of strings containing one or more characters, where any of the characters contained are either alphabetical or space characters, meaning a match on str1 is found but a match on str2 is not found.

There are many more features to regular expressions. An example of its use could be to validate that an e-mail address is of a valid nature, perhaps for an online gaming site account setup. For more on using regular expressions in Java, you should take a look at the method split in the String class and also the classes Pattern and Matcher, which are members of the package java.util.regex.

We will now take a look at the StringBuffer class, which gives us the ability to store and change the string data itself without having to create new String objects every time a different string value is needed.

Legal Disclaimer

Our website is not responsible for the information contained by this article. Webworldarticles.com is a free articles resource thus practically any visitor can submit an article. However if you notice any copyrighted material, please contact us and we will remove the article(s) in discussion right away.


This article was sent to us by: Gabriela C. Perez at 01022008

Related Articles

1. What are Java Listeners
A listener in Java is an object that is used to handle events. In effect, it is implemented to listen for events and then tell the program the required informati...

2. Java EE Platform
Java is a programming language. The Java EE Platform provides a runtime environment (also known as JRE or Java Runtime Environment) as well as a development kit (also known...

3. Java EE Architecture
The Java EE application program interface (API) consists of a suite of technology components and services that are used to build enterprise applications. It includes compon...

4. ISO Management Elements in Java EE .NET Platforms
In a heterogeneous application and platform environment, IT managers are faced with different and often incompatible management frameworks. IT organizations often partiti...

5. Importing Java Packages
To use a package within a Java application or applet, we need to import it. We do this by means of the import keyword. So, for example, if we wish to include the I/O packag...

6. What is a Java Package
A Java package is a collection of related classes that can be imported into your program to support your software. They also provide namespace management, as wel...

7. Character Escape Sequences
Character escape sequences allow for a character to be interpreted differently than its literal value. Character escape sequences are defined using the backslash (\) ch...

8. Conditional Statements
The ability to choose the path that your program takes, based on any given data, is the key to all functionality in programming. In order to create conditional statemen...