Results 1 to 5 of 5
  1. #1
    vnrnithy is offline Member
    Join Date
    Nov 2011
    Posts
    2
    Rep Power
    0

    Default check for a particular pattern

    Hi all,

    i am getting the input from user.

    i want it to be in a particular format as <string>:<integer>

    like example "apples:77" the colan (:) is mandatory.

    prior to the colan string and after the colan integer is also mandatory. i need to check this.

    can you please help me on this???

    - N

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,309
    Blog Entries
    7
    Rep Power
    20

    Default Re: check for a particular pattern

    Maybe regular expressions are the way to solve your problem, e.g. your Strings match the following regular expression: [A-Za-z]+:\\d+ It's unreadable gibberish but it states that at least one letter (uppercase or lowercase) followed by a colon, followed by at least one digit should be the 'format' of your String. Read all about it in the API documentation for the Pattern and Matcher classes.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    vnrnithy is offline Member
    Join Date
    Nov 2011
    Posts
    2
    Rep Power
    0

    Default Re: check for a particular pattern

    thanks jos.

    how to embed this into the code as a Boolean and make it as test condition???

    also if i want to include both string and integer before : can i do it as [A-Za-z0-9]+:\\d+ or [A-Za-z]+\\d+:\\d+???

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,309
    Blog Entries
    7
    Rep Power
    20

    Default Re: check for a particular pattern

    Quote Originally Posted by vnrnithy View Post
    thanks jos.

    how to embed this into the code as a Boolean and make it as test condition???

    also if i want to include both string and integer before : can i do it as [A-Za-z0-9]+:\\d+ or [A-Za-z]+\\d+:\\d+???
    Have you read the mentioned API documentation? It explains the details much better than I could do; concentrate on the Pattern class and the Matcher class.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: check for a particular pattern

    String.matches would also work.

Similar Threads

  1. [Help] url-pattern in web.xml
    By vuonhongchoem in forum Java Servlet
    Replies: 1
    Last Post: 03-27-2012, 04:43 AM
  2. strategy pattern and bridge pattern
    By jomypgeorge in forum New To Java
    Replies: 2
    Last Post: 12-13-2010, 05:13 AM
  3. Class pattern to generate following pattern:-
    By vxs in forum New To Java
    Replies: 5
    Last Post: 07-14-2010, 11:15 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •