• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Regular Expression

 
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 43028
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
     
    Ranch Hand
    Posts: 34
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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 "@#$&%".
     
    Greenhorn
    Posts: 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
     
    Could you hold this puppy for a sec? I need to adjust this tiny ad:
    the value of filler advertising in 2021
    https://coderanch.com/t/730886/filler-advertising
    reply
      Bookmark Topic Watch Topic
    • New Topic