Last active
August 29, 2015 14:14
-
-
Save jkereako/3d0a9e2a4cc531a575a0 to your computer and use it in GitHub Desktop.
Use this code snippet to test your regular expression syntax.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// regex-tester.m | |
// Alexis Digital | |
// | |
// Created by Jeff Kereakoglow on 1/23/15. | |
// | |
// Use this code snippet to test your regular expression syntax. | |
// | |
#import <Foundation/Foundation.h> | |
int main(int argc, const char *argv[]) { | |
@autoreleasepool { | |
NSString *regex; | |
NSString *text; | |
NSPredicate *predicate; | |
BOOL isValid = NO; | |
// Get the command-line arguments | |
if([NSProcessInfo processInfo].arguments.count >= 3) { | |
regex = [NSProcessInfo processInfo].arguments[1]; | |
text = [NSProcessInfo processInfo].arguments[2]; | |
predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex]; | |
isValid = [predicate evaluateWithObject:[text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]]; | |
NSLog(@"\n\n Regex: %@\n Text: %@\n Result: %@\n\n", regex, text, ((isValid) ? @"Valid" : @"Invalid")); | |
} | |
else { | |
NSLog(@"\n\n Not enough arguments"); | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Description
I have a hard time writing regular expressions that are compatible with Cocoa. I wrote this small program to quickly test to see if my syntax is correct.
Compilation
Compile with this command
And execute with this command
To see this result