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

matching an optionally zero-padded number

Last post 05-08-2008, 3:18 AM by clappy. 2 replies.
Sort Posts: Previous Next
  •  05-07-2008, 7:14 AM 41980

    matching an optionally zero-padded number

    Hi

    I'm trying to construct a regex to match some product codes in a particular format.  The format is as follows: A 4 digit year between 1985 and 2038, followed by any number > 0, which may be optionally padded with zeros to exactly 6 digits.  The sequences may appear anywhere in a longer string of any type of unicode characters.

     So the following would be valid:

    20031234

    2003001234

    2000000001

    20001

    20030001234 - should match on 2003000123

    0020031234 - should match on 20031234

    These would not be valid:

    1984001234

    2039001234

    200301234

     An example of the input string would be something like this:

    Lorem ipsum dolor sit amet, consectetuer adipiscing el20031234it. Donec consequat. Aenean dolor diam, imperdiet at, facilisis at, fringilla ut, justo. Morbi sem. Quisque nec sem aliquam nisl euismod ultrices. Vestibulum in eros eget massa rhoncus pellentesque. Ut cursus tellus vitae sapien. Mauris 2003001234 a lorem. Aenean est. Nunc vehicula, nisl ac auctor mollis, lacus lectus condimentum turpis, eu sagittis massa orci id 1924000012350nibh. In molestie ultricies turpis. Donec suscipit, justo eget sodales convallis, magna purus euismod massa, in sodales magna lacus quis sapien. 200003001234Aliquam eleifend, risus sed varius elementum, leo nulla feugiat mi, ut pharetra lorem mauris eget lacus. 

      From which I need to match 20031234 and 2003001234

     

    Can this actually be done or is my spec too vague? 

     

    Thank you very much!!!!

  •  05-07-2008, 9:25 AM 41985 in reply to 41980

    Re: matching an optionally zero-padded number

    (?:19(?:8[5-9]|9\d)|20(?:[0-2]\d|3[0-8]))(?:(?!0{6})(?:0\d{5}|00\d{4}|000\d{3}|0{4}\d\d|0{5}\d|[1-9]\d{1,5}))


  •  05-08-2008, 3:18 AM 42008 in reply to 41985

    Re: matching an optionally zero-padded number

    Brilliant! Thank you very much.
View as RSS news feed in XML