regex - 如何检查字符串是否包含点(句点)?
问题描述
我想找出一个字符串是否包含一个或多个点。这是我的代码:
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>
。
我该如何解决这个问题?
解决方案
.
是正则表达式中匹配任何单个字符的特殊字符。为了从字面上匹配它,它需要被转义:"\\."
。
反斜杠加倍是因为我们需要在字符串文字中转义它的特殊含义。
推荐阅读
- angular - Angular cdk 断点和通用问题
- jenkins - 主 Jenkins 是否跟踪在从节点上创建的动态作业?
- spring - Spring webclient:同时多个请求
- python - 使用 SQLAlchemy/Pandas to_SQL 填充 SQL 表时检查重复
- javascript - 我无法在 TypeScript 中使用 Axios
- python - ggplot2 / plotnine:如何为融化的 df 绘制分组图表?
- javascript - 在添加输入框时动态调整 div 的大小
- python - Python绘图到PDF多个数字
- javascript - JS 中的展开/休息运算符是如何工作的?
- c# - 在视图 asp 中提供标识符后,如何使用主数据部分填充事务视图?