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


GO Regexp.FindStringIndex用法及代碼示例

GO語言"regexp"包中"Regexp.FindStringIndex"類型的用法及代碼示例。

用法:

func(re *Regexp) FindStringIndex(s string)(loc []int)

FindStringIndex 返回一個二元素整數切片,定義正則表達式 s 中最左側匹配的位置。匹配本身位於 s[loc[0]:loc[1]]。返回值 nil 表示不匹配。

例子:

package main

import (
    "fmt"
    "regexp"
)

func main() {
    re := regexp.MustCompile(`ab?`)
    fmt.Println(re.FindStringIndex("tablett"))
    fmt.Println(re.FindStringIndex("foo") == nil)
}

輸出:

[1 3]
true

相關用法


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