如何检查字符串是否包含点(句点)?

时间:2020-10-06 03:16:15

标签: regex go

我想查找一个字符串是否包含一个或多个点。这是我的代码:

package main 
  
import ( 
    "fmt"
    "regexp"
) 
    
func main() { 
    s1 := "Welcome to dotless string"
    p1 := "."
  
    res1, e := regexp.MatchString(p1, s1) 
    fmt.Println("Result and Error is:", res1, e) 
  
} 

但这不起作用:

Result and Error is: true <nil>

它应该返回false <nil>

如何修复this

1 个答案:

答案 0 :(得分:0)

.是正则表达式中与任何单个字符匹配的特殊字符。要从字面上进行匹配,需要对其进行转义:"\\."

反斜杠加倍,因为我们需要在字符串文字中转义其特殊含义。

相关问题