Should i ignore warnings on regex declaration

This is a discussion on Should i ignore warnings on regex declaration within the C++ Programming forums, part of the General Programming Boards category; I have the following declaration Code: /* Validate RSA cell/toll-free number */ bool Rental::cellNoValidator(const std::string& cn) { static const boost::regex ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    610

    Should i ignore warnings on regex declaration

    I have the following declaration


    Code:
    /* Validate RSA cell/toll-free number */
    bool Rental::cellNoValidator(const std::string& cn)
    {
    	static const boost::regex e("[0|\+27]+[1234][123][-| ]?[0-9]{3}[-| ]?([0-9]{4})$");
    	return regex_match(cn, e);
    }
    
    /* Validate RSA land line number */
    bool Rental::landLineValidator(const std::string& lndln)
    {
    	static const boost::regex e("[0|\+27]+[78]([01234]|[68])[-| ]?[0-9]{3}[-| ]?([0-9]{4})$");
    	return regex_match(lndln, e);
    }
    Here are the warnings ..

    Code:
    : warning C4129: '+' : unrecognized character escape sequence
    : warning C4129: '+' : unrecognized character escape sequence
    My validation is not working well. I used Regex online test and these Expressions work well, but not on C++

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    Perhaps you should change [0|\+27] to [0|+27].
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by csonx_p View Post
    I have the following declaration


    Code:
    /* Validate RSA cell/toll-free number */
    bool Rental::cellNoValidator(const std::string& cn)
    {
    	static const boost::regex e("[0|\+27]+[1234][123][-| ]?[0-9]{3}[-| ]?([0-9]{4})$");
    	return regex_match(cn, e);
    }
    
    /* Validate RSA land line number */
    bool Rental::landLineValidator(const std::string& lndln)
    {
    	static const boost::regex e("[0|\+27]+[78]([01234]|[68])[-| ]?[0-9]{3}[-| ]?([0-9]{4})$");
    	return regex_match(lndln, e);
    }
    Here are the warnings ..

    Code:
    : warning C4129: '+' : unrecognized character escape sequence
    : warning C4129: '+' : unrecognized character escape sequence
    My validation is not working well. I used Regex online test and these Expressions work well, but not on C++
    Oh, this is where the problem is, is there anything wrong with this getline()?
    Code:
    	do
    	{
    		cout << "\nWork/Home Number [ccc-nnn-nnnn] : ";
    		getline(cin, landline);
    		cin.ignore();
    	}
    	while ( !landLineValidator(landline) );
    Nothing is read into lanline

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Can you use alternation inside a character class? To me, [0|\+27] means that it should match either a 0, a |, a \, a +, a 2, or a 7. That's probably not what you want. If you want grouping, you should use (parentheses) instead.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by tabstop View Post
    Can you use alternation inside a character class? To me, [0|\+27] means that it should match either a 0, a |, a \, a +, a 2, or a 7. That's probably not what you want. If you want grouping, you should use (parentheses) instead.
    No, it means 0 or '+' followed by 2 & 7 ...

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by csonx_p View Post
    Oh, this is where the problem is, is there anything wrong with this getline()?
    Code:
    	do
    	{
    		cout << "\nWork/Home Number [ccc-nnn-nnnn] : ";
    		getline(cin, landline);
    		cin.ignore();
    	}
    	while ( !landLineValidator(landline) );
    Nothing is read into lanline
    If the cin.ignore() is used to remove unwanted \n characters, it needs to be done before the getline, not after it. (Unwanted \n characters are left in by >> operations, so the ignore function needs to follow those -- getline takes the \n characters as part of the input.)

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by tabstop View Post
    Can you use alternation inside a character class? To me, [0|\+27] means that it should match either a 0, a |, a \, a +, a 2, or a 7. That's probably not what you want. If you want grouping, you should use (parentheses) instead.
    My apologies, you actually correct about using parenthesis

  8. #8
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by tabstop View Post
    If the cin.ignore() is used to remove unwanted \n characters, it needs to be done before the getline, not after it. (Unwanted \n characters are left in by >> operations, so the ignore function needs to follow those -- getline takes the \n characters as part of the input.)
    HI tabstop, i've been doing this wrong anyway, should be

    Code:
    	do
    	{
    		cout << "\nWork/Home Number [ccc-nnn-nnnn] : ";
    		getline(landline, 12); // Read up to 12 charactors
    	}
    But am now getting errors

    Code:
     error C2784: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'std::basic_istream<_Elem,_Traits> &' from 'std::string'
    : see declaration of 'std::getline'

  9. #9
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Ok, am confused

    Code:
    istream& getline (char* s, streamsize n );
    istream& getline (char* s, streamsize n, char delim );
    http://www.cplusplus.com/reference/iostream/istream/getline.html
    Code:
    #include <string>
    istream& getline( istream& is, string& s, char delimiter = '\n' );
    http://www.cppreference.com/cppstring/getline.html


  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by csonx_p View Post
    HI tabstop, i've been doing this wrong anyway, should be

    Code:
    	do
    	{
    		cout << "\nWork/Home Number [ccc-nnn-nnnn] : ";
    		getline(landline, 12); // Read up to 12 charactors
    	}
    But am now getting errors

    Code:
     error C2784: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'std::basic_istream<_Elem,_Traits> &' from 'std::string'
    : see declaration of 'std::getline'
    The "name, number" form of getline only works on C-strings, not std::strings. If you're using std::strings, you need to use getline(cin, landline). (The point being that std::strings automagically lengthen themselves based on what gets typed in, so you don't care how many characters are read in.)

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    cplusplus.com has the other (C++) one as well -- in fact, that's the first one I find when I search cplusplus.com. (It's in string, not in iostream.)

  12. #12
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Is there a reason for this to cause a runtime error after i've entered the input

    Code:
            String landline;
    
    	do
    	{
    		cin.ignore();
    		cout << "\nWork/Home Number [ccc-nnn-nnnn] : ";
    		getline(cin, landline);
    	}
    	while ( !landLineValidator(landline) );
    I've entered "021-460-3084"

  13. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,663
    "String" is not std::string, which is accepted by std::getline.
    Other than that, an obvious question would be - where does the runtime error occur?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by Elysia View Post
    "String" is not std::string, which is accepted by std::getline.
    Other than that, an obvious question would be - where does the runtime error occur?
    Typo ... should be string landline ...

    Other than that, an obvious question would be - where does the runtime error occur?
    Immediately when i press enter after entering the string ... (i have a break point after this line -- at -> while()... )

  15. #15
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by Elysia View Post
    Other than that, an obvious question would be - where does the runtime error occur?
    I think i'm smoking something these days... The Run-Time error happens here
    Code:
    static const boost::regex e("(0|+27)+[78]([01234]|[68])[-| ]?[0-9]{3}[-| ]?([0-9]{4})$");

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  2. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 02:49 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  4. Errors with including winsock 2 lib
    By gamingdl'er in forum C++ Programming
    Replies: 3
    Last Post: 12-05-2005, 07:13 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM

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