當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


GO Match用法及代碼示例

GO語言"path/filepath"包中"Match"函數的用法及代碼示例。

用法:

func Match(pattern, name string)(matched bool, err error)

Match 報告 name 是否與 shell 文件名模式匹配。模式語法是:

pattern:
	{ term }
term:
	'*'         matches any sequence of non-Separator characters
	'?'         matches any single non-Separator character
	'[' [ '^' ] { character-range } ']'
	            character class (must be non-empty)
	c           matches character c (c != '*', '?', '\\', '[')
	'\\' c      matches character c

character-range:
	c           matches character c (c != '\\', '-', ']')
	'\\' c      matches character c
	lo '-' hi   matches character c for lo <= c <= hi

匹配需要模式來匹配所有名稱,而不僅僅是一個子字符串。當模式格式錯誤時,唯一可能返回的錯誤是ErrBadPattern。

在 Windows 上,轉義被禁用。相反,'\\' 被視為路徑分隔符。

例子:

package main

import (
	"fmt"
	"path/filepath"
)

func main() {
	fmt.Println("On Unix:")
	fmt.Println(filepath.Match("/home/catch/*", "/home/catch/foo"))
	fmt.Println(filepath.Match("/home/catch/*", "/home/catch/foo/bar"))
	fmt.Println(filepath.Match("/home/?opher", "/home/gopher"))
	fmt.Println(filepath.Match("/home/\\*", "/home/*"))

}

輸出:

On Unix:
true <nil>
false <nil>
true <nil>
true <nil>

相關用法


注:本文由純淨天空篩選整理自golang.google.cn大神的英文原創作品 Match。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。