Java 1.4 Game Programming (Wordware Game and Graphics Library)
|
|
In order to gain a complete understanding of regular expressions, it would take many pages which we sadly cannot spare in this book, as they are not overly essential to games programming. What we can do, however, is tell you what they are and give you some simple examples from which you can investigate further if you so wish. If, when you read this small subsection, you are completely baffled as to how they work or even what they are, worry not. It is quite likely you may never need to use them, but they can be useful in certain circumstances.
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, which can be useful for searching and manipulating textual data, which is beyond the scope of this book. 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. Packages are discussed later in the book in Chapter 5.
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.
|
|