Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

Revision as of 10:39, 13 August 2013 by hamishwillee (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Regex in Java ME

From Wiki
Jump to: navigation, search

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
Created: maiconherverton (21 Sep 2009)
Last edited: hamishwillee (13 Aug 2013)


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.

   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,, 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!");
This page was last modified on 13 August 2013, at 10:39.
239 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.


Thank you!

We appreciate your feedback.