golang中的正则匹配&判断元素是否在slice里或者数组里

1、正则匹配

package main

import (
	"fmt"
	"regexp"
)

func main() {

    //pattern := "\\d+"     //反斜杠要转义
	pattern := "[a-zA-Z]"   //匹配字母
	str := "a1.22.35.4"
	result, _ := regexp.MatchString(pattern, str)
	fmt.Println(result)   //true
}

2、判断元素是否在slice里或者数组里

package main

import "fmt"

import "github.com/wxnacy/wgo/arrays"

//go get github.com/wxnacy/wgo/arrays   安装包

func main() {
	str := "342"
	var numbers []string
	numbers = append(numbers, "3332", "342", "ssddd", "ssss", "%%%%")

	index := arrays.ContainsString(numbers, str)
	if index == -1 {
		fmt.Printf("not exists") //-1说明不存在
	} else {
		fmt.Println(index) //存在则会返回元素的下标

	}
}

 

你可能感兴趣的:(go)