Go语言提取文本中成对的uid和userPassword值
以下是使用Go语言从文本中提取所有成对的uid和userPassword值的代码示例:/n/ngo/npackage main/n/nimport (/n/t/'fmt/'/n/t/'io/ioutil/'/n/t/'regexp/'/n/t/'strings/'/n)/n/nfunc main() {/n/t// 读取文本文件内容/n/tcontent, err := ioutil.ReadFile('file.txt')/n/tif err != nil {/n/t/tfmt.Println(/'读取文件失败:/', err)/n/t/treturn/n/t}/n/n/t// 将文本内容转换为字符串/n/ttext := string(content)/n/n/t// 使用正则表达式匹配uid和userPassword的值/n/tre := regexp.MustCompile(`uid:(/S+)/nuserPassword:(/S+)`)/n/tmatches := re.FindAllStringSubmatch(text, -1)/n/n/t// 打印匹配结果/n/tfor _, match := range matches {/n/t/tuid := strings.TrimSpace(match[1])/n/t/tuserPassword := strings.TrimSpace(match[2])/n/t/tfmt.Println('uid:', uid)/n/t/tfmt.Println('userPassword:', userPassword)/n/t/tfmt.Println('---------------------')/n/t}/n}/n/n/n请注意,代码中的'file.txt'应替换为您实际的文件路径。运行代码后,将打印出所有匹配到的uid和userPassword的值。
原文地址: https://www.cveoy.top/t/topic/phkU 著作权归作者所有。请勿转载和采集!