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.
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
1. What are Java Listeners
All articles in this directory are property of their respective authors. Additionally, read our Privacy Policy
© 2010 WebWorldarticles.com - All Rights Reserved. Partners: Gunblade Saga