当前位置: 首页 > news >正文

Go语言学习记录——用正则表达式(regexp包)来校验参数

前言

最近坐毕设ing,简单的一个管理系统。
其中对于用户注册、登录功能,需要进行一些参数校验。
因为之前使用过,因此这里计划使用正则表达式进行校验。但是之前的使用也仅限于使用,因此这次专门进行一次学习,并做此记录。

什么是正则表达式

下面是菜鸟教程中给出的定义

正则表达式是一种用于匹配和操作文本的强大工具,它是由一系列字符和特殊字符组成的模式,用于描述要匹配的文本模式。
正则表达式可以在文本中查找、替换、提取和验证特定的模式。

简单来说,他就是一个文字处理的工具,对文字进行一系列的处理。在Golang中,可以使用内置的regexp包来进行使用。

参数校验

使用MatchString

这里使用的是MatchString函数.


// MatchString reports whether the string s
// contains any match of the regular expression pattern.
// More complicated queries need to use Compile and the full Regexp interface.
func MatchString(pattern string, s string) (matched bool, err error) {re, err := Compile(pattern)if err != nil {return false, err}return re.MatchString(s), nil
}// Compile parses a regular expression and returns, if successful,
// a Regexp object that can be used to match against text.
//
// When matching against text, the regexp returns a match that
// begins as early as possible in the input (leftmost), and among those
// it chooses the one that a backtracking search would have found first.
// This so-called leftmost-first matching is the same semantics
// that Perl, Python, and other implementations use, although this
// package implements it without the expense of backtracking.
// For POSIX leftmost-longest matching, see CompilePOSIX.
func Compile(expr string) (*Regexp, error) {return compile(expr, syntax.Perl, false)
}// MatchString reports whether the string s
// contains any match of the regular expression re.
func (re *Regexp) MatchString(s string) bool {return re.doMatch(nil, nil, s)
}

根据该函数的源码和注释可以看出:其需要接受两个参数——校验规则pattern和待处理字符串s,其返回两个值——matched 是一个布尔值,表示是否匹配成功,err 是一个错误值,表示在匹配过程中是否出现了错误。

在函数内部,它首先使用 Compile 函数将 pattern 编译成一个 Regexp 对象。如果编译过程中出现错误,就会直接返回错误。如果编译成功,它会调用编译后的 Regexp 对象的 MatchString 方法来对字符串 s 进行匹配,最终将匹配结果返回。

校验规则

拿自己代码中用到的来举例

	passwordPattern = `^[a-zA-Z0-9]{6,12}$`

这个代表的是:参数a-z,A-Z,0-9,且长度在6-12位之间。
其他标识符规则如下:

.: 匹配任意单个字符,除了换行符。
*: 匹配前面的表达式零次或多次。
+: 匹配前面的表达式一次或多次。
?: 匹配前面的表达式零次或一次。
[]: 字符类,匹配括号内的任意一个字符。
|: 或操作符,匹配两边任意一个表达式。
(): 分组,用于将多个表达式组合在一起。

参考资源

本次学习主要参考自:

Golang-regexp包官方文档
https://pkg.go.dev/regexp

http://www.lryc.cn/news/275737.html

相关文章:

  • 公司办公电脑文件防泄密系统
  • 手把手带你死磕ORBSLAM3源代码(三十四)Tracking.cc MonocularInitialization编辑
  • STL标准库与泛型编程(侯捷)笔记3
  • Iceberg: 列式读取Parquet数据
  • Ansible、Saltstack、Puppet自动化运维工具介绍
  • python线程池提交任务
  • 跨境电商企业客户服务优化指南:关键步骤与实用建议
  • Visual Studio Code 常用快捷键
  • ubuntu创建pytorch-gpu的docker环境
  • 数据库原理与应用期末复习试卷2
  • 操作系统丨单元测试
  • tcp/ip协议2实现的插图,数据结构6 (24 - 章)
  • Linux链接的创建,删除,修改
  • HarmoryOS Ability页面的生命周期
  • 【Flink 从入门到成神系列 一】算子
  • 无人机自主寻优降落在移动车辆
  • 科技感十足界面模板
  • pytest装饰器 @pytest.mark.parametrize 使用方法
  • redis被攻击
  • 二手买卖、废品回收小程序 在app.json中声明permission scope.userLocation字段 教程说明
  • 【AI视野·今日Sound 声学论文速览 第四十期】Wed, 3 Jan 2024
  • Unity组件开发--升降梯
  • 插槽slot涉及到的样式污染问题
  • OpenCV-Python(25):Hough直线变换
  • python接口自动化(七)--状态码详解对照表(详解)
  • Android 实现动态申请各项权限
  • 【leetcode】力扣热门之合并两个有序列表【简单难度】
  • 安全与认证Week3 Tutorial+历年题补充
  • 【Kotlin】协程
  • Scikit-Learn线性回归(五)