Golang基础-正则表达式
backticks
When using backticks (`) to make strings(Raw string literals), backslashes (\
) don't have any special meaning and don't mark the beginning of special characters like tabs \t
or newlines \n
:
- 可以多行
- 反斜杠不表示转义,仅表示自己
- 因为正则表达式里有很多反斜杠,这样就不用一直转义反斜杠了
常用函数
- [, 标记一个中括号表达式的开始。要匹配 [,请使用 [。
- +, 匹配前面的子表达式一次或多次。要匹配 + 字符,请使用 +。
- \d, 匹配一个数字字符。等价于 [0-9]。
- *, 匹配前面的子表达式零次或多次。例如,zo* 能匹配 "z" 以及 "zoo"。* 等价于{0,}。
所以上面的正则表达式[a-z]+\d*
表示a到z出现一次或多次,数字出现零次或多次。
FindStringSubmatch
从字符串中提取符合要求的子串。
- (), 标记一个子表达式的开始和结束位置。子表达式可以获取供以后使用。要匹配这些字符,请使用 \( 和 \)。
也就是说把要提取内容的正则表达式放在括号里就能提出来了。
返回一个string slice,第一个元素是整个匹配结果,后面是匹配到的每个分组
Exercise
- Task1
You need some idea of how many log lines in your archive do not comply with current standards. You believe that a simple test reveals whether a log line is valid. To be considered valid a line should begin with one of the following strings:
[TRC]
[DBG]
[INF]
[WRN]
[ERR]
[FTL] - Task2
A new team has joined the organization, and you find their log files are using a strange separator for "fields". Instead of something sensible like a colon ":" they use a string such as "<--->" or "<=>" (because it's prettier) in fact any string that has a first character of "<" and a last character of ">" and any combination of the following characters "~", "*", "=" and "-" in between. - Task3
The team needs to know about references to passwords in quoted text so that they can be examined manually. - Task4
You have found that some upstream processing of the logs has been scattering the text "end-of-line" followed by a line number (without an intervening space) throughout the logs. - Task5
You have noticed that some of the log lines include sentences that refer to users. These sentences always contain the string "User", followed by one or more space characters, and then a user name. You decide to tag such lines.
__EOF__

本文作者:RoadWide
本文链接:https://www.cnblogs.com/roadwide/p/17138137.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角【推荐】一下。您的鼓励是博主的最大动力!
本文链接:https://www.cnblogs.com/roadwide/p/17138137.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角【推荐】一下。您的鼓励是博主的最大动力!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具