URL Validation Problem!

Discussion in 'Mac Programming' started by saleh.hi.62, Nov 22, 2011.

  1. saleh.hi.62 macrumors member

    Joined:
    Jul 25, 2011
    #1
    hello guys,

    i am trying to validate URL with this method:

    Code:
    - (BOOL) validateUrl: (NSString *) candidate {
    	
    	NSString *urlRegEx=
    	@"((https?|ftp|gopher|telnet|file|notes|ms-help):((//)|(\\\\))+[\w\d:#@%/;$()~_?\+-=\\\.&]*)";
        
    	NSPredicate *urlTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", urlRegEx]; 
        return [urlTest evaluateWithObject:candidate];
    	
    }
    it does not function! i think problem is with Regular expression !
     
  2. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #2
    Because Objective-C doesn't have special syntax for writing regular expressions, but instead reuses the general string syntax, for every backslash in a regular expression you need to write two backslashes in the string.

    You haven't done that, at least not in the character class at the end of the regex.
     
  3. saleh.hi.62 thread starter macrumors member

    Joined:
    Jul 25, 2011
    #3
    Code:
    ((https?|ftp|gopher|telnet|file|notes|ms-help):((//)|(\\\\\\\\))+[\w\d:#@%/;$()~_?\\+-=\\\\\\.&]*)
    i am using this now according to what you said, but still it does not work!
     
  4. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #4
    And what about \w and \d? These need to be \\w and \\d.

    BTW I haven't actually been through your regex to see if it's actually correct for not. I'd just noticed these "syntax" errors.
     
  5. saleh.hi.62 thread starter macrumors member

    Joined:
    Jul 25, 2011
    #5
    thank you my friend :) it already works :apple:
     

Share This Page