• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regular Expression

 
Rohit Bhagwat
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rohit.
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
     
    Uma Mahi
    Ranch Hand
    Posts: 34
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    var illegalChars= /[_ \(\)\<\>\,\;\:\\\/\"\[\]]/
    if (strng.match(illegalChars)) // string is the form input value
    return false;
    else
    return true;
    =====================
     
    Garrett Rowe
    Ranch Hand
    Posts: 1296
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by Uma Mahi:
    var illegalChars= /[_ \(\)\<\>\,\;\:\\\/\"\[\]]/
    if (strng.match(illegalChars)) // string is the form input value
    return false;
    else
    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
    Greenhorn
    Posts: 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Garrett Rowe is spot on

    Try this:

    ^(\d*)([a-zA-Z]+)([A-Za-z]|\d)*$

    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:
    http://jakarta.apache.org/regexp/applet.html

    Good Luck
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic