Regular Expression..

This is a discussion on Regular Expression.. within the Tech Board forums, part of the Community Boards category; Can anyone help me formulate a regular expression for the following The valid entries are 49837659465 65986749569435 85645094 +564385639 +4234 ...

  1. #1
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683

    Regular Expression..

    Can anyone help me formulate a regular expression for the following

    The valid entries are


    49837659465
    65986749569435
    85645094
    +564385639
    +4234
    +1
    +68465945


    So anything with just numbers or a + in front of the numbers is valid...

    thanx in advance....

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,786
    hmm... assuming it is Perl compatible, what about
    /^\+[0-9]+$/
    or
    /^\+\d+$/
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    thanx.... I better learn regular expressions properly.. seems really usefull..

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,786
    hehe, coming back to this thread I just realised I gave a wrong pattern and no one corrected me.

    Those patterns would not match the digits only expressions.

    You should use:
    /^\+?\d+$/

    I'm using slashes as pattern delimiters, while ^ indicates the start, $ the end.
    \+ is a literal plus symbol, having been escaped.
    ? means 0 or 1 instances of the character/subpattern that precedes it.
    \d should be equivalent to [0-9], meaning a character from 0 to 9.
    then + means 1 or more instances of the character/subpattern that precedes it.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. recursion error
    By cchallenged in forum C Programming
    Replies: 2
    Last Post: 12-18-2006, 08:15 AM
  3. Regular Expression
    By tintifaxe in forum C++ Programming
    Replies: 3
    Last Post: 06-14-2006, 07:16 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Regular Expression Troubles
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 04-11-2002, 04:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21