regex multiline mode
Fix the regex pattern in line A, to replace all consecutive 0s to one 0 when 0 is the beginning of the line.The expected output is like:1000101
package main
import (
"fmt"
"regexp"
)
func main() {
s := `100
00
0
1
0
0
1`
pattern := regexp.MustCompile("0(0|\n)*0") //A
s = pattern.ReplaceAllString(s, "0")
fmt.Println(s)
}
Answer
package main
import (
"fmt"
"regexp"
)
func main() {
s := `100
00
0
1
0
0
1`
pattern := regexp.MustCompile("(?m:^0(0|\n)*0)")
s = pattern.ReplaceAllString(s, "0")
fmt.Println(s)
}