C
const char *str = "2016-12-05";
regex_t rx;
if (regcomp(&rx, "([[:digit:]]{4})-([[:digit:]]{2})-([[:digit:]]{2})", REG_EXTENDED)==0) {
regmatch_t m[4];
if (regexec(&rx, str, 4, m, 0)==0) {
int year = str2int(str+m[1].rm_so, m[1].rm_eo - m[1].rm_so);
int month = str2int(str+m[2].rm_so, m[2].rm_eo - m[2].rm_so);
int day = str2int(str+m[3].rm_so, m[3].rm_eo - m[3].rm_so);
}
regfree(&rx);
}
C++
string str("2016-12-05");
regex rx("(\\d{4})-(\\d{2})-(\\d{2})");
smatch m;
if (regex_match(str, m, rx)) {
int year = stoi(m.str(1));
int month = stoi(m.str(2));
int day = stoi(m.str(3));
}
CoffeeScript
str = '2016-12-05'
if /(\d{4})-(\d{2})-(\d{2})/.test str
year = Number RegExp.$1
month = Number RegExp.$2
day = Number RegExp.$3
Java
String str = "2016-12-05";
Pattern rx = Pattern.compile("(\\d{4})-(\\d{2})-(\\d{2})");
Matcher m = rx.matcher(str);
if (m.matches()) {
int year = Integer.parseInt(m.group(1));
int month = Integer.parseInt(m.group(2));
int day = Integer.parseInt(m.group(3));
}
JavaScript
var str = '2016-12-05';
if (/(\d{4})-(\d{2})-(\d{2})/.test(str)) {
var year = Number(RegExp.$1);
var month = Number(RegExp.$2);
var day = Number(RegExp.$3);
}
Kotlin
val str = "2016-12-05"
val rx = "(\\d{4})-(\\d{2})-(\\d{2})".toRegex()
val m = rx.matchEntire(str)
if (m!=null) {
val year = m.groups[1]!!.value.toInt()
val month = m.groups[2]!!.value.toInt()
val day = m.groups[3]!!.value.toInt()
}
Lua
str = '2016-12-05'
rx = '(%d%d%d%d)-(%d%d)-(%d%d)'
year, month, day = string.match(str, rx)
year = tonumber(year)
month = tonumber(month)
day = tonumber(day)
Objective-C
NSString *str = @"2016-12-05";
NSRegularExpression *rx = [NSRegularExpression regularExpressionWithPattern:@"(\\d{4})-(\\d{2})-(\\d{2})" options:0 error:nil];
NSTextCheckingResult *m = [rx firstMatchInString:str options:0 range:NSMakeRange(0, [str length])];
if (m!=nil) {
NSInteger year = [[str substringWithRange:[m rangeAtIndex:1]] integerValue];
NSInteger month = [[str substringWithRange:[m rangeAtIndex:2]] integerValue];
NSInteger day = [[str substringWithRange:[m rangeAtIndex:3]] integerValue];
}
Perl
$str = '2016-12-05';
$rx = qr/(\d{4})-(\d{2})-(\d{2})/;
if ($str =~ $rx) {
($year, $month, $day) = (int $1, int $2, int $3);
}
PHP
$str = '2016-12-05';
$rx = '/(\d{4})-(\d{2})-(\d{2})/';
if (preg_match($rx, $str, $m)) {
$year = (int)$m[1];
$month = (int)$m[2];
$day = (int)$m[3];
list($_, $year, $month, $day) = $m;
}
Python
str = '2016-12-05'
rx = '(\\d{4})-(\\d{2})-(\\d{2})'
m = re.search(rx, str)
if m:
year = int(m.group(1))
month = int(m.group(2))
day = int(m.group(3))
year, month, day = m.groups()
Ruby
str = '2016-12-05'
rx = /(\d{4})-(\d{2})-(\d{2})/
m = rx.match(str)
if m
year = m[1].to_i
month = m[2].to_i
day = m[3].to_i
year, month, day = m[1..3]
end
str = '2016-12-05'
rx = /(\d{4})-(\d{2})-(\d{2})/
if rx =~ str
year = $~[1].to_i
month = $~[2].to_i
day = $~[3].to_i
year, month, day = $~[1..3]
end
Swift
let str = "2016-12-05"
let rx = try! NSRegularExpression(pattern: "(\\d{4})-(\\d{2})-(\\d{2})", options: [])
if let m = rx.firstMatch(in: str, options: [], range: NSRange(location: 0, length: (str as NSString).length)) {
let year = Int((str as NSString).substring(with: m.rangeAt(1)))
let month = Int((str as NSString).substring(with: m.rangeAt(2)))
let day = Int((str as NSString).substring(with: m.rangeAt(3)))
}