Skip to content

Instantly share code, notes, and snippets.

@rickytan
Last active January 31, 2019 03:50
Show Gist options
  • Save rickytan/66f70b5c8bcec22050562fb10a650f93 to your computer and use it in GitHub Desktop.
Save rickytan/66f70b5c8bcec22050562fb10a650f93 to your computer and use it in GitHub Desktop.
A Macro to generate regular expression
#ifndef REGEXP
# define _regexp_stringify(x) #x
# define _regexp_stringify2(x) _regexp_stringify(x)
# define _regexp(...) ({ const char *str = _regexp_stringify2(# __VA_ARGS__); const size_t length = strlen(str); [[NSString alloc] initWithBytes:str + 1 length:length - 2 encoding:NSUTF8StringEncoding]; })
# define REGEXP(...) [NSRegularExpression regularExpressionWithPattern:_regexp(__VA_ARGS__) options:0 error:NULL]
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment