File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Regular Expression Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Regular Expression" Watch "Regular Expression" New topic

Regular Expression

Rohit Bhagwat
Ranch Hand

Joined: Dec 19, 2004
Posts: 205
Hello sir, madam, friends

Actually I am unable to construct a single regular expression for the following requirement.Can you please help me out ??

Requirement :
Input string can contain only alphabets and numbers. No special characters are allowed. Also there must be atleast one character in the input string. That means input string should not contain only numbers.

I am trying to come out with a single regular expression but unable to do so? Is this possible in a single regular expression ?

Waiting for your replies.
Thanks and Regards
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
This sounds like a homework assignment, so I'll just give some hints. Start by looking at the javadocs for the java.util.Pattern class. Try to figure out how what \p{L} and \p{Digit} do. Then construct a regexp that contains only letters and digits, and has at least length 1. The last setp would to require at least one letter, but needn't have any digits.
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Its possible, creating regular expressions (for me at least) involves truly breaking down the requirements. From those requirements a legal expression would have:
  • 0 or more digits
  • 1 or more characters [a-zA-Z]
  • 0 or more digits
  • And the entire preceding pattern can repeat itself one or more times.

  • Now all thats left is to translate that into a Java regular expression.

    Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
    Uma Mahi
    Ranch Hand

    Joined: Jan 11, 2006
    Posts: 34
    var illegalChars= /[_ \(\)\<\>\,\;\:\\\/\"\[\]]/
    if (strng.match(illegalChars)) // string is the form input value
    return false;
    return true;

    Umaa Mahi<br />--SCJP1.4
    Garrett Rowe
    Ranch Hand

    Joined: Jan 17, 2006
    Posts: 1296
    Originally posted by Uma Mahi:
    var illegalChars= /[_ \(\)\<\>\,\;\:\\\/\"\[\]]/
    if (strng.match(illegalChars)) // string is the form input value
    return false;
    return true;

    This pseudomethod does not address all of the OP's requirements. Specifically this method would return true for an input of all digits, "123", or any characters that werent included in your illegalChars variable "@#$&%".
    Julio Nguyen

    Joined: Mar 06, 2006
    Posts: 1
    Garrett Rowe is spot on

    Try this:


    What it says:
    - May start with a number ^(\d*)
    ^ = beginning of line
    \d = digit 0-9
    * = 0 or more
    - Must have at least one letter ([a-zA-Z]+)
    a-zA-Z = any letter
    + = 1 or more
    - Then ends with any number of letters or numbers ([A-Za-z]|\d)*$
    $ = end of string

    A great place to test your RegEx patterns is:

    Good Luck
    I agree. Here's the link:
    subject: Regular Expression
    It's not a secret anymore!