Regex in Java ME
This article demonstrates the use of Java ME regular expressions (Regex) into match patterns in user-submitted text input. For instance, using Regex it is possible to block undesired input characters like: "!@#$%¨&*(".
Article Metadata
Contents |
What are Regex?
Regex provide a concise mechanism to search and match patterns in text strings. They are useful for many text parsing activities, including ensuring that input matches (or does not match) certain patterns.
Regex in Java ME
For using Regex in Java ME it is necessary to use a third party package like me.regexp .*;. This package can be obtained from regexp-me library and it is based on Package Jakarta Regexp version for J2SE
Regex Syntax
Regular expressions are written using a number of special characters or meta character. They are summarized below.
Characters
unicodeChar Matches any identical unicode character \ Used to quote a meta-character (like '*') \\ Matches a single '\' character \0nnn Matches a given octal character \xhh Matches a given 8-bit hexadecimal character \\uhhhh Matches a given 16-bit hexadecimal character \t Matches an ASCII tab character \n Matches an ASCII newline character \r Matches an ASCII return character \f Matches an ASCII form feed character
Character Classes
[abc] Simple character class [a-zA-Z] Character class with ranges [^abc] Negated character class
NOTE: Incomplete ranges will be interpreted as "starts from zero" or "ends with last character". I.e. [-a] is the same as [\\u0000-a], and [a-] is the same as [a-\\uFFFF], [-] means "all characters".
Standard POSIX Character Classes
[:alnum:] Alphanumeric characters.
[:alpha:] Alphabetic characters.
[:blank:] Space and tab characters.
[:cntrl:] Control characters.
[:digit:] Numeric characters.
[:graph:] Characters that are printable and are also visible.
(A space is printable, but not visible, while an
`a' is both.)
[:lower:] Lower-case alphabetic characters.
[:print:] Printable characters (characters that are not
control characters.)
[:punct:] Punctuation characters (characters that are not letter,
digits, control characters, or space characters).
[:space:] Space characters (such as space, tab, and formfeed,
to name a few).
[:upper:] Upper-case alphabetic characters.
[:xdigit:] Characters that are hexadecimal digits.
Non-standard POSIX-style Character Classes
[:javastart:] Start of a Java identifier [:javapart:] Part of a Java identifier
Predefined Classes
. Matches any character other than newline \w Matches a "word" character (alphanumeric plus "_") \W Matches a non-word character \s Matches a whitespace character \S Matches a non-whitespace character \d Matches a digit character \D Matches a non-digit character
Boundary Matchers
^ Matches only at the beginning of a line $ Matches only at the end of a line \b Matches only at a word boundary \B Matches only at a non-word boundary
Greedy Closures
A* Matches A 0 or more times (greedy)
A+ Matches A 1 or more times (greedy)
A? Matches A 1 or 0 times (greedy)
A{n} Matches A exactly n times (greedy)
A{n,} Matches A at least n times (greedy)
A{n,m} Matches A at least n but not more than m times (greedy)
Reluctant Closures
A*? Matches A 0 or more times (reluctant) A+? Matches A 1 or more times (reluctant) A?? Matches A 0 or 1 times (reluctant)
Logical Operators
AB Matches A followed by B
A|B Matches either A or B
(A) Used for subexpression grouping
(?:A) Used for subexpression clustering (just like grouping but
no back references)
Back references
\1 Back reference to 1st parenthesized subexpression \2 Back reference to 2nd parenthesized subexpression \3 Back reference to 3rd parenthesized subexpression \4 Back reference to 4th parenthesized subexpression \5 Back reference to 5th parenthesized subexpression \6 Back reference to 6th parenthesized subexpression \7 Back reference to 7th parenthesized subexpression \8 Back reference to 8th parenthesized subexpression \9 Back reference to 9th parenthesized subexpression
Regex example for Java ME
/* Author: Maicon Herverton, maiconherverton@yahoo.com.br, Brazil */
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import me.regexp.*;
public class Regex extends MIDlet {
public Regex() {}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {}
protected void pauseApp() {}
protected void startApp() throws MIDletStateChangeException {
//Here is the text that you want to check
String text = "abcdef232";
//In this case we are creating the standard to be validated the text.
RE pattern = RE("abc(\\w*)");
if (pattern.matcher(text)) {
//If the text is within the standard
System.out.println("The text: " + text + " is the standard!");
}
else {
//If the text is not within the standard
System.out.println("The text: " + text + " is not in default!");
}
}
}


28 Sep
2009
Using REGEX is very important for users to achieve in only 1 or 2 lines of code perform a complex operation that in most cases requires a huge number of lines of code, saving time, effort and cost.
This article discusses the use of regular expressions in Java ME. Java ME does not provide built-in support for regular expressions. The article links to a third party regular expression for Java ME which can be downloaded. The majority of the article is devoted to outlining the notation in which regular expressions can be written. The notation should be familiar to those who have used regular expressions in Java SE. Finally, a code example is given, showing how to import the regular expression library and a basic example of its use.
Because of its lightweight nature, Java ME does not provide any support for regular expressions. This API therefore provides a useful extension for applications where regular expressions can save a lot of time and simplify code. Without regular expressions, code for parsing text from files or downloaded via the Internet can get rather messy and difficult to read and maintain. The article doesn’t spend a lot of time explaining the API, but it is fairly simple and straightforward to use. All the reader really needs is to know how to write regular expressions to match different kinds of patterns.