Regular Expressions in Java

by Gabriela C. Perez.

Share
|
Homepage | Submit your article | Contact | TOS
More articles on java  

You are here: Categories » Computers and technology » 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.

Leave a comment or ask a question
Total comments: 0

JAVA Disclaimer

  • The e-articles directory is not responsible for any and all copyright infringements by writers and authors. If you suspect the information contained by this page for any copyright infringements, please contact us to investigate the issue
Arithmetic Assignment Operators - The following assignment operators are similar to the increment and decrement operators that we have just seen. They are used so that you do not need to enter the source variable twice when ass (more...)
Bitwise Operators - The following table shows the standard bitwise operators in Java and a description of them. (more...)
Invocation Chaining - Invocation chaining means that you are not limited to merely accessing one class/object member in a given statement with the . operator but may continue to access further members in a given stateme (more...)
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 (\) character, followed by th (more...)
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 statements, we must first learn a (more...)
Java Methods - Methods are used as the building blocks of your program, performing tasks that can be called again and again and using the same code to perform the task each time. The basic but fundamental parts o (more...)
Variable Scope - The scope of a variable is the area in which a variable belongs, specified by the area in which it is declared. The following example code contains two declared variables, one inside a code block a (more...)
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 well as access protection. (more...)
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 package, which is called java.i (more...)
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 partition the heterogeneous pl (more...)

 
free content
    Copyright © 2006 - 2012 e-articles.info.
The texts, articles and tutorials in the directory are property of their respective owners and authors.