欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

正则匹配字母开头,包含字母、数字、下划线,长度大于等于6,小于等于15的字符串

程序员文章站 2024-02-24 13:48:52
...

正则匹配字母开头,包含字母、数字、下划线,长度大于等于6,小于等于15的字符串

正则表达式:
^(?=[a-zA-z]+)(?=.\d+.)(?=.[a-z|A-Z]+.)(?=._.)[\d(a-z|A-Z)_]{6,10}$

golang形式:

package main

import (
	"fmt"
	"regexp"
)

func main() {
	str := ""
	matched, err := regexp.MatchString("^(?=[a-zA-z]+)(?=.*\\d+.*)(?=.*[a-z|A-Z]+.*)(?=.*_.*)[\\d(a-z|A-Z)_]{6,10}$", str)
	fmt.Println(matched, err)
}

PHP匹配:

$str = '';
$isMatched = preg_match('/^(?=[a-zA-z]+)(?=.*\d+.*)(?=.*[a-z|A-Z]+.*)(?=.*_.*)[\d(a-z|A-Z)_]{6,10}$/', $str, $matches);
var_dump($isMatched, $matches);

JS(JavaScript)匹配

var pattern = /^(?=[a-zA-z]+)(?=.*\d+.*)(?=.*[a-z|A-Z]+.*)(?=.*_.*)[\d(a-z|A-Z)_]{6,10}$/,
	str = '';
console.log(pattern.test(str));

java匹配

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexMatches {
	
	public static void main(String args[]) {
		String str = "";
		String pattern = "^(?=[a-zA-z]+)(?=.*\\d+.*)(?=.*[a-z|A-Z]+.*)(?=.*_.*)[\\d(a-z|A-Z)_]{6,10}$";

		Pattern r = Pattern.compile(pattern);
		Matcher m = r.matcher(str);
		System.out.println(m.matches());
	}

}

Ruby匹配

pattern = /^(?=[a-zA-z]+)(?=.*\d+.*)(?=.*[a-z|A-Z]+.*)(?=.*_.*)[\d(a-z|A-Z)_]{6,10}$/
str = ''
p pattern.match(str)

python匹配

import re
pattern = re.compile(ur'^(?=[a-zA-z]+)(?=.*\d+.*)(?=.*[a-z|A-Z]+.*)(?=.*_.*)[\d(a-z|A-Z)_]{6,10}$')
str = u''
print(pattern.search(str))