本文整理汇总了Golang中regexp.MatchString函数的典型用法代码示例。如果您正苦于以下问题:Golang MatchString函数的具体用法?Golang MatchString怎么用?Golang MatchString使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了MatchString函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: clear
func clear() error {
if err := filepath.Walk("public", func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if m, _ := regexp.MatchString("~$", path); m {
return nil
}
// Remove auto-generated html files.
m, err := regexp.MatchString(".html$", path)
if err != nil {
return err
}
if m {
return os.Remove(path)
}
// Remove example resources that are copied.
m, err = regexp.MatchString("^public/examples/_resources/images$", path)
if err != nil {
return err
}
if m {
if err := os.RemoveAll(path); err != nil {
return err
}
return filepath.SkipDir
}
return nil
}); err != nil {
return err
}
return nil
}
示例2: newProjectStep
func newProjectStep(data *WizardData) {
fmt.Print("Enter name of new project: ")
projectParam := &phraseapp.ProjectParams{}
fmt.Scanln(&projectParam.Name)
res, err := phraseapp.ProjectCreate(projectParam)
if err != nil {
success, match_err := regexp.MatchString("401", err.Error())
if match_err != nil {
fmt.Println(err.Error())
panic(match_err.Error())
}
if success {
data.AccessToken = ""
DisplayWizard(data, "", fmt.Sprintf("Argument Error: Your AccessToken '%s' has no write scope. Please create a new Access Token with read and write scope.", data.AccessToken))
} else {
success, match_err := regexp.MatchString("Validation failed", err.Error())
if match_err != nil {
fmt.Println(err.Error())
panic(match_err.Error())
}
if success {
DisplayWizard(data, "newProject", err.Error())
return
} else {
panic(err.Error())
}
}
} else {
data.ProjectId = res.Id
DisplayWizard(data, next(data), "")
return
}
}
示例3: getBody
// 根据网址 获取内容,确定是否需要进一步处理
func getBody(url string) (string, string, error) {
//根据url首先判断是否读取
if strings.Contains(url, ".hqew.com/ic") {
res, err := http.Get(url)
if err != nil {
fmt.Println("connect error")
log.Fatal(err)
}
robots, err := ioutil.ReadAll(res.Body)
if err != nil {
fmt.Println("connect error")
log.Fatal(err)
}
res.Body.Close()
body := string(robots)
if len(body) == 0 {
fmt.Println("null body")
}
//
isMatch_stock, _ := regexp.MatchString(
`xh ic_ico1`, body)
isMatch_black, _ := regexp.MatchString(
`target="_blank" class="g_fb"`, body)
//isMatch := isMatch_stock || isMatch_black
if isMatch_stock == true {
return body, "stock", nil
} else if isMatch_black == true {
return "NotStock", "black", nil
}
return "", "false", nil
} else {
return "", "false", nil
}
}
示例4: main
func main() {
if len(os.Args) != 2 {
fmt.Println("Please select one picture to makeascii...")
return
}
fileName := os.Args[1]
isJpeg, _ := regexp.MatchString(".+\\.jpg", fileName)
isPng, _ := regexp.MatchString(".+\\.png", fileName)
var picture image.Image
var imageErr error
fileIn, errIn := os.Open(fileName)
if errIn != nil {
fmt.Println(errIn.Error())
return
}
if isJpeg {
picture, imageErr = jpeg.Decode(fileIn)
} else if isPng {
picture, imageErr = png.Decode(fileIn)
} else {
fmt.Println("File type is not supported...")
return
}
if imageErr != nil {
fmt.Println(imageErr.Error())
return
}
fmt.Print(MakeAscii(GetImage(picture)))
}
示例5: TestAudit
func TestAudit(t *testing.T) {
var buf bytes.Buffer
attributeGetter := apiserver.NewRequestAttributeGetter(&fakeRequestContextMapper{},
&apiserver.RequestInfoResolver{APIPrefixes: sets.NewString("api", "apis"), GrouplessAPIPrefixes: sets.NewString("api")})
handler := WithAudit(&fakeHTTPHandler{}, attributeGetter, &buf)
req, _ := http.NewRequest("GET", "/api/v1/namespaces/default/pods", nil)
req.RemoteAddr = "127.0.0.1"
handler.ServeHTTP(httptest.NewRecorder(), req)
line := strings.Split(strings.TrimSpace(buf.String()), "\n")
if len(line) != 2 {
t.Fatalf("Unexpected amount of lines in audit log: %d", len(line))
}
match, err := regexp.MatchString(`[\d\:\-\.\+TZ]+ AUDIT: id="[\w-]+" ip="127.0.0.1" method="GET" user="admin" as="<self>" asgroups="<lookup>" namespace="default" uri="/api/v1/namespaces/default/pods"`, line[0])
if err != nil {
t.Errorf("Unexpected error matching first line: %v", err)
}
if !match {
t.Errorf("Unexpected first line of audit: %s", line[0])
}
match, err = regexp.MatchString(`[\d\:\-\.\+TZ]+ AUDIT: id="[\w-]+" response="200"`, line[1])
if err != nil {
t.Errorf("Unexpected error matching second line: %v", err)
}
if !match {
t.Errorf("Unexpected second line of audit: %s", line[1])
}
}
示例6: TestMutexProfile
func TestMutexProfile(t *testing.T) {
old := runtime.SetMutexProfileFraction(1)
defer runtime.SetMutexProfileFraction(old)
if old != 0 {
t.Fatalf("need MutexProfileRate 0, got %d", old)
}
blockMutex()
var w bytes.Buffer
Lookup("mutex").WriteTo(&w, 1)
prof := w.String()
if !strings.HasPrefix(prof, "--- mutex:\ncycles/second=") {
t.Errorf("Bad profile header:\n%v", prof)
}
prof = strings.Trim(prof, "\n")
lines := strings.Split(prof, "\n")
if len(lines) != 6 {
t.Errorf("expected 6 lines, got %d %q\n%s", len(lines), prof, prof)
}
if len(lines) < 6 {
return
}
// checking that the line is like "35258904 1 @ 0x48288d 0x47cd28 0x458931"
r2 := `^\d+ 1 @(?: 0x[[:xdigit:]]+)+`
//r2 := "^[0-9]+ 1 @ 0x[0-9a-f x]+$"
if ok, err := regexp.MatchString(r2, lines[3]); err != nil || !ok {
t.Errorf("%q didn't match %q", lines[3], r2)
}
r3 := "^#.*runtime/pprof_test.blockMutex.*$"
if ok, err := regexp.MatchString(r3, lines[5]); err != nil || !ok {
t.Errorf("%q didn't match %q", lines[5], r3)
}
}
示例7: checkFunParam
func checkFunParam(fun type_sqlfun) {
switch fun.name {
case "regsub":
if len(fun.params) != 3 {
parseError(fun.name + " function params error")
}
re := regexp.MustCompile("^\\$\\d{1,3}")
if !re.MatchString(fun.params[0]) {
parseError(fun.name + " function params error")
}
case "strsub":
if len(fun.params) > 3 || len(fun.params) < 2 {
parseError(fun.name + " function params num error")
}
m, _ := regexp.MatchString("^\\$\\d{1,3}", fun.params[0])
fp("^\\$\\d{1,3}")
if !m {
parseError(fun.name + " function params error")
}
case "count,sum,avg,max,min":
if len(fun.params) != 1 {
parseError(fun.name + " function params error")
}
m, _ := regexp.MatchString("^\\$\\d{1,3}", fun.params[0])
if !m {
parseError(fun.name + " function params error")
}
}
}
示例8: GetMetricName
func (c *CollectdJSON) GetMetricName(index int) string {
metricName := c.Host + "_" + c.Plugin
if len(c.PluginInstance) > 0 {
if matched, _ := regexp.MatchString(`^\d+$`, c.PluginInstance); matched {
metricName += c.PluginInstance
} else {
metricName += "_" + c.PluginInstance
}
}
tSize := len(c.Type)
pSize := len(c.Plugin)
if tSize > 0 {
if pSize <= tSize && c.Type[:pSize] == c.Plugin {
metricName += c.Type[pSize:]
} else {
metricName += "." + c.Type
}
}
if len(c.TypeInstance) > 0 {
if matched, _ := regexp.MatchString(`^\d+$`, c.TypeInstance); matched {
metricName += c.TypeInstance
} else {
metricName += "_" + c.TypeInstance
}
}
if c.DataSetNames[index] != "value" {
metricName += "." + c.DataSetNames[index]
}
return metricName
}
示例9: detectProdConfig
func detectProdConfig(useosxt bool) string {
var levelUp string
var curDir string
sep := string(filepath.Separator)
if useosxt {
curDir, _ = os.Getwd()
} else {
curDir, _ = osext.ExecutableFolder()
}
//detect from test or console
match, _ := regexp.MatchString("_test", curDir)
matchArgs, _ := regexp.MatchString("arguments", curDir)
matchTestsDir, _ := regexp.MatchString("tests", curDir)
if match || matchArgs || matchTestsDir {
if matchTestsDir {
levelUp = ".."
}
curDir, _ = filepath.Abs(curDir + sep + levelUp + sep)
}
CurrDirectory = curDir
configDir, _ := filepath.Abs(curDir + sep + CONFIG_DIR + sep)
appConfig := configDir + sep + CONFIG_FILE
appProdConfig := configDir + sep + PRODUCTION_FOLDER + sep + CONFIG_FILE
if fileExists(appProdConfig) {
appConfig = appProdConfig
} else if !useosxt {
appConfig = detectProdConfig(true)
}
return appConfig
}
示例10: TestUserSetRoleWithNoVal_SystemTest
func TestUserSetRoleWithNoVal_SystemTest(t *testing.T) {
for _, dut := range duts {
cmds := []string{
"no username test",
"username test role network-admin nopassword",
}
if ok := dut.Config(cmds...); !ok {
t.Fatalf("dut.Config() failure")
}
user := User(dut)
config := user.GetSection()
if found, _ := regexp.MatchString(`username test privilege 1 role network-admin nopassword`, config); !found {
t.Fatalf("\"username test privilege 1\" expected but not seen under "+
"user section.\n[%s]", config)
}
if ok := user.SetRole("test", ""); !ok {
t.Fatalf("SetRole config for user failed")
}
config = user.GetSection()
if found, _ := regexp.MatchString(`username test privilege 1 role network-admin nopassword`, config); found {
t.Fatalf("\"username test privilege 1 role network-admin nopasswd\" expected but not seen under "+
"user section.\n[%s]", config)
}
}
}
示例11: TestUserSetSSHKeyWithNoVal_SystemTest
func TestUserSetSSHKeyWithNoVal_SystemTest(t *testing.T) {
for _, dut := range duts {
cmds := []string{
"no username test",
"username test nopassword",
}
if ok := dut.Config(cmds...); !ok {
t.Fatalf("dut.Config() failure")
}
user := User(dut)
config := user.GetSection()
if found, _ := regexp.MatchString(`username test privilege 1 nopassword`, config); !found {
t.Fatalf("\"username test privilege 1\" expected but not seen under "+
"user section.\n[%s]", config)
}
if ok := user.SetSshkey("test", ""); !ok {
t.Fatalf("SetSshkey config for user failed")
}
config = user.GetSection()
configStr := "username test sshkey " + regexp.QuoteMeta(testSSHKey)
if found, _ := regexp.MatchString(configStr, config); found {
t.Fatalf("\"%s\" NOT expected but not seen under "+
"user section.\n[%s]", configStr, config)
}
}
}
示例12: TestUserSetPrivWithInvalidVal_SystemTest
func TestUserSetPrivWithInvalidVal_SystemTest(t *testing.T) {
for _, dut := range duts {
cmds := []string{
"no username test",
"username test privilege 8 nopassword",
}
if ok := dut.Config(cmds...); !ok {
t.Fatalf("dut.Config() failure")
}
user := User(dut)
config := user.GetSection()
if found, _ := regexp.MatchString(`username test privilege 8 nopassword`, config); !found {
t.Fatalf("\"username test privilege 8\" expected but not seen under "+
"user section.\n[%s]", config)
}
if ok, err := user.SetPrivilege("test", 65535); ok || err == nil {
t.Fatalf("SetPrivilege config for user failed")
}
config = user.GetSection()
if found, _ := regexp.MatchString(`username test privilege 8 nopassword`, config); !found {
t.Fatalf("\"username test privilege 8 nopasswd\" expected but not seen under "+
"user section.\n[%s]", config)
}
}
}
示例13: TestUserDelete_SystemTest
func TestUserDelete_SystemTest(t *testing.T) {
for _, dut := range duts {
cmds := []string{
"no username test",
"username test nopassword",
}
if ok := dut.Config(cmds...); !ok {
t.Fatalf("dut.Config() failure")
}
user := User(dut)
config := user.GetSection()
if found, _ := regexp.MatchString(`username test privilege 1 nopassword`, config); !found {
t.Fatalf("\"username test privilege 1\" expected but not seen under "+
"user section.\n[%s]", config)
}
if ok := user.Delete("test"); !ok {
t.Fatalf("Delete of user failed")
}
config = user.GetSection()
if found, _ := regexp.MatchString(`username test privilege 1 nopassword`, config); found {
t.Fatalf("\"username test privilege 1\" expected but not seen under "+
"user section.\n[%s]", config)
}
}
}
示例14: TestUserCreateWithSecret_SystemTest
func TestUserCreateWithSecret_SystemTest(t *testing.T) {
for _, dut := range duts {
cmds := []string{
"no username test",
}
if ok := dut.Config(cmds...); !ok {
t.Fatalf("dut.Config() failure")
}
user := User(dut)
config := user.GetSection()
if found, _ := regexp.MatchString(`username test privilege 1`, config); found {
t.Fatalf("\"username test privilege 1\" NOT expected but not seen under "+
"user section.\n[%s]", config)
}
if ok, err := user.CreateWithSecret("test", "password", "cleartext"); !ok || err != nil {
t.Fatalf("CreateWithSecret of user failed")
}
config = user.GetSection()
if found, _ := regexp.MatchString(`username test`, config); !found {
t.Fatalf("\"username test privilege 1\" expected but not seen under "+
"user section.\n[%s]", config)
}
}
}
示例15: parseStartLink
func parseStartLink() {
fmt.Println("Input url: ")
fmt.Scanf("%s", &url)
firstDoc, err := goquery.NewDocument(url)
checkerr(err)
firstDoc.Find("tbody").Each(func(i int, tbody *goquery.Selection) {
tbody.Find(".description").Each(func(j int, s *goquery.Selection) {
link, _ := s.Find("a").Attr("href")
x, _ := regexp.MatchString(`https://www.exploit-db.com/exploits/.....`, link)
if x == true {
file, err := os.OpenFile("temp.txt", os.O_CREATE|os.O_RDWR|os.O_APPEND, 0666)
checkerr(err)
_, err = file.WriteString(link + "\n")
checkerr(err)
file.Close()
}
y, _ := regexp.MatchString(`/docs/......pdf`, link)
if y == true {
wasteUrl, err := os.OpenFile("waste.txt", os.O_CREATE|os.O_RDWR|os.O_APPEND, 0666)
checkerr(err)
_, err = wasteUrl.WriteString(link)
checkerr(err)
wasteUrl.Close()
}
})
})
}