需要从字符串中提取单词的帮助.
STRING | WORD NEEDS TO BE FETCHED |
---|---|
"A = 2&& B = 2 && func1(A) && func2(B)" | func1, func2 |
"func1(A)" | func1 |
"B = 2 && func2(A)" | func2 |
我try 过遵循regex
package main
import (
"fmt"
"regexp"
)
func main() {
var re = regexp.MustCompile(`\s+(?:\w+_\w+)`)
var str = `func1(A)`
for i, match := range re.FindAllString(str, -1) {
fmt.Println(match, "found at index", i)
}
}
但如果单词eg:对于字符串"func1(A)"来说,之前没有空间,则该regex提供空结果.
当单词前面有一个空位时,这会按预期工作,例如对于字符串"B = 2 func2(A)",它会按预期给出func2.
如果有任何帮助,我们将不胜感激