Results 1 to 7 of 7
  1. #1
    stuckonjava is offline Senior Member
    Join Date
    Jan 2012
    Posts
    148
    Rep Power
    0

    Default Regular expression question

    Produce a regular expression to describe each of the families of strings described below! Expression of time of the form “7.15am” or “11.34pm”

    What I got was

    (\d|\d\d) \. \d\d (am|pm)

    But I'm not sure whether this is correct, could someone inform me if it is.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Regular expression question

    Quote Originally Posted by stuckonjava View Post
    Produce a regular expression to describe each of the families of strings described below! Expression of time of the form “7.15am” or “11.34pm”

    What I got was

    (\d|\d\d) \. \d\d (am|pm)

    But I'm not sure whether this is correct, could someone inform me if it is.
    What happened when you put this in a small program and then threw a bunch of test Strings at it?

  3. #3
    stuckonjava is offline Senior Member
    Join Date
    Jan 2012
    Posts
    148
    Rep Power
    0

    Default Re: Regular expression question

    Quote Originally Posted by Fubarable View Post
    What happened when you put this in a small program and then threw a bunch of test Strings at it?
    I haven't learnt how to test them yet, I just wanted to get some practice in writing them fluently, is what I did correct?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Regular expression question

    It is one possible way to write this, but I think that you may have the cart before the horse -- read up on how to test your regexes before trying to create them, since your Java compiler is much quicker at answering these types of questions than a forum. Also look into using {1,2} notation for your first 1-2 character number rather than a group.

  5. #5
    stuckonjava is offline Senior Member
    Join Date
    Jan 2012
    Posts
    148
    Rep Power
    0

    Default Re: Regular expression question

    Quote Originally Posted by Fubarable View Post
    It is one possible way to write this, but I think that you may have the cart before the horse -- read up on how to test your regexes before trying to create them, since your Java compiler is much quicker at answering these types of questions than a forum. Also look into using {1,2} notation for your first 1-2 character number rather than a group.
    Thanks for your reply.

    Could I just quickly ask what [01]?\d means for the first part of the time before the '.'

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Regular expression question

    Quote Originally Posted by stuckonjava View Post
    Thanks for your reply.

    Could I just quickly ask what [01]?\d means for the first part of the time before the '.'
    Have a look here: Regular Expressions Tutorial: repeat

    The whole tutorial is excellent and one you should link to: Regular Expressions Tutorial

  7. #7
    stuckonjava is offline Senior Member
    Join Date
    Jan 2012
    Posts
    148
    Rep Power
    0

    Default Re: Regular expression question

    I've read through it all now, thanks for the link.

    There is however, one case which I'm findind difficult to get my head around.

    The regular expression for any email address is:
    \w(\.?[\w\-]+)*@\w+(\.[\w\-]+)+

    Could anyone help break this down for me , thanks

Similar Threads

  1. regular expression
    By ehsansad in forum New To Java
    Replies: 8
    Last Post: 03-18-2011, 02:23 PM
  2. Help with regular expression
    By mr.ab18 in forum New To Java
    Replies: 2
    Last Post: 08-06-2010, 10:01 PM
  3. regular expression
    By prof.deedee in forum JDBC
    Replies: 3
    Last Post: 02-19-2010, 11:15 AM
  4. regular expression
    By QkrspCmptPop in forum Advanced Java
    Replies: 8
    Last Post: 01-20-2010, 03:55 AM
  5. regular expression
    By ras_pari in forum Advanced Java
    Replies: 27
    Last Post: 10-07-2009, 12:25 PM

Posting Permissions

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