Results 1 to 9 of 9
  1. #1
    QkrspCmptPop is offline Member
    Join Date
    Aug 2009
    Posts
    9
    Rep Power
    0

    Default regular expression

    In java regular expression, is it possible to reference one previous part of this pattern, or is it possible to make the comparison of two parts of a pattern.

    For example, here is the Days Starttime-Endtime pattern
    ^(M?T?W?R?F?S?U?\\s(([01]\\d|2[0-3])[0-5]\\d\\-([01]\\d|2[0-3])[0-5]\\d))$

    here is a valid office hours:
    MWF 0800-1000

    here should be an invalid office hours:
    MWF 1100-1000
    Because endtime should be after start time.

    The real question is:
    can we implement start time before end time through pattern?

    Thanks.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    I don't think with a RegEx this is possible, may be in a complex manner. Why don't you do simple string manipulation, separate the start-end times and process.

  3. #3
    QkrspCmptPop is offline Member
    Join Date
    Aug 2009
    Posts
    9
    Rep Power
    0

    Default

    Yep. Seems that one possible solution is to get the capturing groups with matcher.group, then doing the comparison between 2 groups and throw the exception

  4. #4
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    Sorry dude, but like Eranga said, I don't think it's possible. I looked over some conditional, lookahead, lookbehind and there's nothing that would check if a backreference is greater than another backreference
    "Experience is what you get when you don't get what you want" (Dan Stanford)
    "Rise and rise again until lambs become lions" (Robin Hood)

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,335
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Lil_Aziz1 View Post
    Sorry dude, but like Eranga said, I don't think it's possible. I looked over some conditional, lookahead, lookbehind and there's nothing that would check if a backreference is greater than another backreference
    Indeed, regular expressions can't do any numerical calculations; e.g. see the mess how to specify a number in the range [0,23]. REs can recognize regular patterns in Strings. But on top of those REs there's Java that can do the non-regular condition checks.

    kind regards,

    Jos

  6. #6
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    yea it does look like a mess, but regex's (in my opinion) autonomy is just prodigious. It doesn't rely on a programming language. It's like an interface and is implemented by many languages. Regex is probably one of the things that I love about programming in general.
    "Experience is what you get when you don't get what you want" (Dan Stanford)
    "Rise and rise again until lambs become lions" (Robin Hood)

  7. #7
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Quote Originally Posted by Lil_Aziz1 View Post
    yea it does look like a mess, but regex's (in my opinion) autonomy is just prodigious. It doesn't rely on a programming language. It's like an interface and is implemented by many languages. Regex is probably one of the things that I love about programming in general.
    Try to understand its limitations though. Days have been wasted at trying to devise a regex for a non regular language. The pumping lemma thinks regex is for kids.

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,335
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by r035198x View Post
    Try to understand its limitations though. Days have been wasted at trying to devise a regex for a non regular language. The pumping lemma thinks regex is for kids.
    Note that this baby pumping lemma can decide whether or not a language is regular; if so a regular expression can be crafted to recognize this (baby) language. A textual representation for two numbers n1 and n2 where n2 > n1 definitely is not regular.

    kind regards,

    Jos

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by JosAH View Post
    Note that this baby pumping lemma can decide whether or not a language is regular; if so a regular expression can be crafted to recognize this (baby) language. A textual representation for two numbers n1 and n2 where n2 > n1 definitely is not regular.
    I've seen that most the time people try to solve such complex validations using RegEx. In my experience they feel that RegEx more powerful to do any kind of validations, in string manipulations.

Similar Threads

  1. regular expression
    By ras_pari in forum Advanced Java
    Replies: 27
    Last Post: 10-07-2009, 12:25 PM
  2. Basic regular expression help
    By predhme in forum New To Java
    Replies: 1
    Last Post: 06-26-2009, 01:29 AM
  3. need help to make this regular expression
    By aruna1 in forum New To Java
    Replies: 1
    Last Post: 04-12-2009, 08:09 PM
  4. Regular Expression Challange
    By hiklior in forum New To Java
    Replies: 2
    Last Post: 04-24-2008, 05:05 AM
  5. regular expression for unicode
    By tharhan in forum Advanced Java
    Replies: 0
    Last Post: 04-01-2008, 10:53 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
  •