自动化测试
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

67 lines
2.2 KiB

package re
import (
"regexp"
"github.com/dlclark/regexp2"
)
/*常用的正则表达式*/
const (
Number = `\d+`
)
/****************************************************************************
* Function Name : FindAll
* Description : 匹配字符串,返回原串和匹配到的串 不支持真相匹配
* Input : exp 正则表达式 str需要匹配的字符串
* Output : None
* Return : None
****************************************************************************/
func FindAll(exp, str string) [][]string {
p := regexp.MustCompile(exp).FindAllStringSubmatch(str, -1)
return p
}
/****************************************************************************
* Function Name : MatchString
* Description : 检查字符串
* Input : exp 正则表达式 str需要匹配的字符串
* Output : None
* Return : None
****************************************************************************/
func MatchString(exp, str string) bool {
match, _ := regexp.MatchString(exp, str)
return match
}
/****************************************************************************
* Function Name : FindAllString
* Description : 匹配字符串,返回匹配到的字符串 不支持正向匹配
* Input : exp 正则表达式 str需要匹配的字符串
* Output : None
* Return : None
****************************************************************************/
// func FindAllString(exp string, str string) []string {
// p := regexp.MustCompile(exp).FindAllStringSubmatch(str, -1)
// return p[0]
// }
/****************************************************************************
* Function Name : FindAllString
* Description : 匹配字符串,返回匹配到的字符串 支持正向匹配(建议使用正向匹配时采用)
* Input : exp 正则表达式 str需要匹配的字符串
* Output : None
* Return : None
****************************************************************************/
func FindAll2(exp string, str string) []string {
var matches []string
reg, _ := regexp2.Compile(exp, 0)
data, _ := reg.FindStringMatch(str)
for data != nil {
matches = append(matches, data.String())
data, _ = reg.FindNextMatch(data)
}
return matches
}