測試Go語言的regexp包

爲了編寫go 語言下控制NB iot 的AT 命令解釋程序,使用了GO 語言功能強大的regexp 包。本博文記錄了有關學習筆記。

測試程序

package main

import (
	"fmt"
	"regexp"
)
func main(){
	
	match, _ := regexp.MatchString("Hello.*", "Hello The World") 
	fmt.Println(match)
	//
	match, _ = regexp.MatchString("Hello.*|The.*", "Hello The World") 
	fmt.Println(match)
	//
	regExpPatttern := regexp.MustCompile("ERROR.*|MIPLOBSERVE:.*")
	result := regExpPatttern.FindAllString("MIPLOBSERVE:0,0,0\n", -1)
	if len(result)>0 {
        fmt.Print(result[0]+"\n")
	}
	result = regExpPatttern.FindAllString("ERROR\n", -1)
	if len(result)>0 {
        fmt.Print(result[0]+"\n")
	}
	
}

運行結果

true
true
MIPLOBSERVE:0,0,0
ERROR

下次,將Golang 控制 BC95 Onenet 模塊的控制程序放出來。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章