简体   繁体   中英

How to check private/local IP address by using regular expression

Hope all of you will be fine. I am building an IOS application in which I warn the user if they typed a private/local IP address in text-field. I have searched on internet and found it done in the Android app using regular expression: Java: (127.0.0.1)|(192.168.$) | (172.1[6-9].$) | (172.2[0-9].$) | (172.3[0-1].$) | (10.*$)

I want the same regular expression for IOS application but I don't know how to code it. I searched on internet and found Private IP Address Identifier in Regular Expression but i could not understand it. I just know some objective-c. Can some one help me in this regard please.

Ok guys finally I think I have solved the problem by following code.

 -(void)CheckIP { NSError *error = NULL; NSString *pattern = @"(127.0.0.1)|(192.168.$)|(172.1[6-9].$)|(172.2[0-9].$)|(172.3[0-1].$)|(10.*$)"; // "[a-zA-Z]+[,]\\\\s*([AZ]{2})"; NSString *string = self.tfExternalHost.text; NSRange range = NSMakeRange(0, string.length); NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:&error]; NSArray *matches = [regex matchesInString:string options:NSMatchingProgress range:range]; NSLog(@" Found Match %@", matches); } 

Suggest me if the above code can be improved.

I have solved the problem.

-(BOOL)CheckIPAddress
{
    // this code is to check either user entered local/private ip-address
    NSError *error = NULL;    
    NSString *pattern = @"((127\.)|(10\.)|(172\.1[6-9]\.)|(172\.2[0-9]\.)|(172\.3[0-1]\.)|(192\.168\.))";
    NSString *string = self.tfExternalHost.text;
    NSRange range = NSMakeRange(0, string.length);
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:&error];
    NSArray *matches = [regex matchesInString:string options:NSMatchingProgress range:range];
    if (matches.count>0) {
        UIAlertView *simpleAlert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Warning", nil) message:NSLocalizedString(@"Please provide valid external IP address.", nil) delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [simpleAlert show];
        return true;
    }
    return false;
}

I needed IPv6 support as well, so here's my answer. Also, note that iOS regexs should have escaped backslashes as Antoine pointed out.

Here's a category for NSString which returns YES if it's a local / private IP address. Note that this code assumes the string is a valid IP address (ie. it will match 192.168.chocolate.sundae)

    - (BOOL)isPrivateIPAddress {
        NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(^127\\.)|(^192\\.168\\.)|(^10\\.)|(^172\\.1[6-9]\\.)|(^172\\.2[0-9]\\.)|(^172\\.3[0-1]\\.)|(^::1$)|(^[fF][cCdD])" options:0 error:nil];
        NSArray *matches = [regex matchesInString:self options:0 range:NSMakeRange(0, self.length)];
        if (matches.count > 0)
            return YES;
        return NO;
    }

Buyer-beware: I only did limited testing of this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM