Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

Validate if there are two consecutive integers.

Last post 05-13-2008, 11:01 AM by ddrudik. 9 replies.
Sort Posts: Previous Next
  •  05-12-2008, 12:08 AM 42118

    Validate if there are two consecutive integers.

    I building a XSD scheme and would like to create a regular expression the validates two consecutive two-digit integers seperated by a forward slash:

    Valid:
    00/01
    06/07
    29/30

    Invalid:
    01/03

    I'm currently just using
    <xs:pattern value="\d{2}/\d{2}"/>

    which, of course, just checks for two digits.  I have no clue how to check for consecutive.  TIA  --Sam

  •  05-12-2008, 10:02 AM 42125 in reply to 42118

    Re: Validate if there are two consecutive integers.

    regex does not do evaluations of values. It matches patterns, like yours \d{2}. U need to create a functions that wuld take two params from yur regex: for examle :

    024 and 025

    then CONVERT(024) to INTEGER, CONVERT(025) to INTEGER

    then caclulate difference between two numbers: IF 1 then TRUE: u have two consecutive integers.

  •  05-12-2008, 10:42 AM 42126 in reply to 42118

    Re: Validate if there are two consecutive integers.

    http://regexadvice.com/blogs/mash/archive/2007/06/01/Are-you-ready-for-regex_3F00_.aspx

    Michael

    "In theory, theory and practice are the same. In practice, they are not."
    Albert Einstein
  •  05-12-2008, 2:13 PM 42133 in reply to 42118

    Re: Validate if there are two consecutive integers.

    <xs:pattern value="(00/01)|(01/02)|(02/03)|(03/04)|(04/05)|(05/06)|(06/07)|(07/08)|(08/09)|(09/00)"/>

    Unsure if you want to consider 09/00 as a match.


  •  05-12-2008, 3:24 PM 42134 in reply to 42133

    Re: Validate if there are two consecutive integers.

    ddrudik:

    <xs:pattern value="(00/01)|(01/02)|(02/03)|(03/04)|(04/05)|(05/06)|(06/07)|(07/08)|(08/09)|(09/00)"/>

    Unsure if you want to consider 09/00 as a match.

    According to the somewhat vague specs and the provided sample you'd need a pattern that went up to 98/99.


    Michael

    "In theory, theory and practice are the same. In practice, they are not."
    Albert Einstein
  •  05-12-2008, 4:32 PM 42138 in reply to 42134

    Re: Validate if there are two consecutive integers.

    This should meet that requirement:

    <xs:pattern value="(00/01)|(01/02)|(02/03)|(03/04)|(04/05)|(05/06)|(06/07)|(07/08)|(08/09)|(09/10)|(10/11)|(11/12)|(12/13)|(13/14)|(14/15)|(15/16)|(16/17)|(17/18)|(18/19)|(19/20)|(20/21)|(21/22)|(22/23)|(23/24)|(24/25)|(25/26)|(26/27)|(27/28)|(28/29)|(29/30)|(30/31)|(31/32)|(32/33)|(33/34)|(34/35)|(35/36)|(36/37)|(37/38)|(38/39)|(39/40)|(40/41)|(41/42)|(42/43)|(43/44)|(44/45)|(45/46)|(46/47)|(47/48)|(48/49)|(49/50)|(50/51)|(51/52)|(52/53)|(53/54)|(54/55)|(55/56)|(56/57)|(57/58)|(58/59)|(59/60)|(60/61)|(61/62)|(62/63)|(63/64)|(64/65)|(65/66)|(66/67)|(67/68)|(68/69)|(69/70)|(70/71)|(71/72)|(72/73)|(73/74)|(74/75)|(75/76)|(76/77)|(77/78)|(78/79)|(79/80)|(80/81)|(81/82)|(82/83)|(83/84)|(84/85)|(85/86)|(86/87)|(87/88)|(88/89)|(89/90)|(90/91)|(91/92)|(92/93)|(93/94)|(94/95)|(95/96)|(96/97)|(97/98)|(98/99)"/>

    If non-capturing groups are supported I would recommend the following for efficiency:

    <xs:pattern value="(?:00/01)|(?:01/02)|(?:02/03)|(?:03/04)|(?:04/05)|(?:05/06)|(?:06/07)|(?:07/08)|(?:08/09)|(?:09/10)|(?:10/11)|(?:11/12)|(?:12/13)|(?:13/14)|(?:14/15)|(?:15/16)|(?:16/17)|(?:17/18)|(?:18/19)|(?:19/20)|(?:20/21)|(?:21/22)|(?:22/23)|(?:23/24)|(?:24/25)|(?:25/26)|(?:26/27)|(?:27/28)|(?:28/29)|(?:29/30)|(?:30/31)|(?:31/32)|(?:32/33)|(?:33/34)|(?:34/35)|(?:35/36)|(?:36/37)|(?:37/38)|(?:38/39)|(?:39/40)|(?:40/41)|(?:41/42)|(?:42/43)|(?:43/44)|(?:44/45)|(?:45/46)|(?:46/47)|(?:47/48)|(?:48/49)|(?:49/50)|(?:50/51)|(?:51/52)|(?:52/53)|(?:53/54)|(?:54/55)|(?:55/56)|(?:56/57)|(?:57/58)|(?:58/59)|(?:59/60)|(?:60/61)|(?:61/62)|(?:62/63)|(?:63/64)|(?:64/65)|(?:65/66)|(?:66/67)|(?:67/68)|(?:68/69)|(?:69/70)|(?:70/71)|(?:71/72)|(?:72/73)|(?:73/74)|(?:74/75)|(?:75/76)|(?:76/77)|(?:77/78)|(?:78/79)|(?:79/80)|(?:80/81)|(?:81/82)|(?:82/83)|(?:83/84)|(?:84/85)|(?:85/86)|(?:86/87)|(?:87/88)|(?:88/89)|(?:89/90)|(?:90/91)|(?:91/92)|(?:92/93)|(?:93/94)|(?:94/95)|(?:95/96)|(?:96/97)|(?:97/98)|(?:98/99)"/>


  •  05-12-2008, 5:18 PM 42139 in reply to 42138

    Re: Validate if there are two consecutive integers.

    If you want to consolidate this a bit, and the language supports it, you can do something like this:

    (?:(\d)0\/(?:\1)1)|(?:(\d)1\/(?:\2)2)

    This matches x0/x1 and x1/x2 for x between 0 and 9 inclusive.  9 of these total plus 10 or so checks for the transition periods (aka 09/10, 19/20, etc) should do the same job in a bit less text.

  •  05-12-2008, 5:19 PM 42140 in reply to 42138

    Re: Validate if there are two consecutive integers.

    I'm much too lazy too do all that typing. Sleep

    Michael

    "In theory, theory and practice are the same. In practice, they are not."
    Albert Einstein
  •  05-12-2008, 5:25 PM 42141 in reply to 42139

    Re: Validate if there are two consecutive integers.

    Lyndar:

    If you want to consolidate this a bit, and the language supports it, you can do something like this:

    (?:(\d)0\/(?:\1)1)|(?:(\d)1\/(?:\2)2)

    This matches x0/x1 and x1/x2 for x between 0 and 9 inclusive.  9 of these total plus 10 or so checks for the transition periods (aka 09/10, 19/20, etc) should do the same job in a bit less text.

    I don't think XML Schema supports backreferenes or non-capture groups.


    Michael

    "In theory, theory and practice are the same. In practice, they are not."
    Albert Einstein
  •  05-13-2008, 11:01 AM 42181 in reply to 42140

    Re: Validate if there are two consecutive integers.

    mash:
    I'm much too lazy too do all that typing. Sleep

    MS Excel fill series/concatenate did the work.


View as RSS news feed in XML