1 Star 0 Fork 0

Paulden/Algorithm

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
regexp.go 918 Bytes
一键复制 编辑 原始数据 按行查看 历史
Paulden 提交于 2022-08-22 10:30 . update readme; implement asterisk match
package string
import (
"bufio"
"fmt"
"os"
"regexp"
)
// "Hello{ACD}Wor{D}ld" -> "HelloWorld"
// "{..}" -> ""
func replace(src string) string {
return replaceHelper(src, "")
}
func replaceHelper(src, des string) string {
reg := regexp.MustCompile(`{\w*}`)
return reg.ReplaceAllString(src, des)
}
// Searches for (regular) expressions in text files
// Every single line is read and if the line matches the pattern provided on the command line,
// that line is printed
func grep(re, filename string) {
regex, err := regexp.Compile(re)
if err != nil {
panic("The regular expressions is wrong in syntax")
}
fh, err := os.Open(filename)
if err != nil {
panic("Failed to open file: " + filename)
}
defer fh.Close()
f := bufio.NewReader(fh)
var buf []byte
for {
buf, _, err = f.ReadLine()
if err != nil {
return
}
s := string(buf)
if regex.MatchString(s) {
fmt.Println(s)
}
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/guuzaa/algorithm.git
git@gitee.com:guuzaa/algorithm.git
guuzaa
algorithm
Algorithm
main

搜索帮助