身份证分为一代身份证和二代身份证,一代身份证为15位、二代身份证为18位。
举例一代身份证:130503670401001
二代身份证:41140219891216321
一代身份证正则截取:^(\d{6})(\d{6})(.*)$
二代身份证正则截取: ^(\d{6})(\d{8})(.*)$
下面分别设java和golang的截取实现
package spring; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * Created by banxia on 16/6/20. */ public class Test { public static void main(String[] args){ // String str = "411402198912166321"; String str = "130503670401001"; // ^(\d{6})(\d{8})(.*) 18位 //^(\d{6})(\d{6})(.*) 15位 Pattern pattern = Pattern.compile("^(\\d{6})(\\d{6})(.*)"); Matcher matcher = pattern.matcher(str); if (matcher.find()) { String group = matcher.group(2); System.out.print(group); } } }
package main import ( ) import ( "regexp" "fmt" ) func main() { var str string = "411402198912166321" reg,err := regexp.Compile("^(\\d{6})(\\d{8})(.*)") if err != nil { return } if reg.MatchString(str) == true { submatch := reg.FindStringSubmatch(str) fmt.Println(submatch) fmt.Println("date is ",submatch[2]) } }