其他教程

其他教程

Products

当前位置:首页 > 其他教程 >

Go中字符串末尾的正则表达式匹配失败

GG网络技术分享 2025-03-18 16:15 3


问题描述:

I\'m trying to test a string against this pattern: \"At least one square bracket pair, wrapping 2 digits, followed by at lease one character\". For example, [11][22][33]dd should match while [11][22][33] shouldn\'t.

I\'ve tried this regex: (\\[\\d{2}])+.+. However, when it is tested against with [11][22][33], which should have failed, it still passes that test. The first + quantifier only matches two groups [11] and [22], while the rest part [33] is matched by .+.

I thought the \"greedy\" behaviour of the + quantifier would exhaust all the matching segments of the group it modifies; however it seems that the regex engine would place the \"exhaust all matching possibilities\" principle above the \"greedy quantifier\" rule, not the way I\'d expected.

How should I achieve my goal?

(This question is actually language-agnostic, though tagged with \"golang\" which is the language I\'m currently using.)

图片转代码服务由CSDN问答提供

感谢您的意见,我们尽快改进~

功能建议

我正在尝试针对此模式测试字符串:“至少一对方括号,包含两位数字, 其次是至少一个字符”。 例如, [11] [22] [33] dd </ code>应该匹配,而 [11] [22] [33] </ code>不应该匹配。</ p>

\\ n

我已经尝试过以下正则表达式:(\\ [\\ d {2}])+。+ </ code>。 但是,当使用 [11] [22] [33] </ code>进行测试(该测试应该失败了)时,它仍然可以通过该测试。 第一个 + </ code>量词仅匹配两组 [11] </ code>和 [22] </ code>,而其余部分为 [33] </ code >被。+ </ code>匹配。</ p>

我认为 + </ code>量词的“贪婪”行为将耗尽所有的 修改的组; 但是,正则表达式引擎似乎会将“用尽所有匹配可能性”原则置于“贪婪量词”规则之上,而不是我期望的方式。</ p>

我应该如何实现我的目标 ?</ p>

(此问题实际上与语言无关,尽管标记为“ golang”,这是我当前使用的语言。)</ p>

</ div>

网友观点:

You may use

re := regexp.MustCompile(`(?:\\[\\d{2}])+(.*)`)

match := re.FindStringSubmatch(s)

if len(match) > 1 {

return match[1] != \\\"\\\"

}

return false

The (?:\\[\\d{2}])+(.*) pattern matches 1+ occurrences of [, 2 digits, ] and then captures any 0 or more chars other than line break chars into Group 1. Then, if the match was found (if len(match) > 1), true should be returned if the Group 1 value is not empty (match[1] != \\\"\\\"), otherwise, false is returned.

See Go demo:

package main

import (

\\\"fmt\\\"

\\\"regexp\\\"

)

func main() {

strs := []string{

\\\"[11][22][33]\\\",

\\\"___[11][22][33]\\\",

\\\"[11][22][33]____\\\",

\\\"[11][22]____[33]\\\",

}

for _, str := range strs {

fmt.Printf(\\\"%q - %t

\\\", str, match(str))

}

}

var re = regexp.MustCompile(`(?:\\[\\d{2}])+(.*)`)

func match(s string) bool {

match := re.FindStringSubmatch(s)

if len(match) > 1 {

return match[1] != \\\"\\\"

}

return false

}

Output:

\\\"[11][22][33]\\\" - false

\\\"___[11][22][33]\\\" - false

\\\"[11][22][33]____\\\" - true

\\\"[11][22]____[33]\\\" - true

the regular expression you need to use is i presume goes like that:

(\\[[0-9]{2}\\]){1,}[a-z]{1,}

Go语言中正则表达式的处理

1 概述

正则表达式,又称规则表达式,Regular Expression,在代码中常简写为 regex、regexp 或 RE。正则表达式是对字符串操作的一种逻辑公式,就是用事先定义好的一些特定字符、及这些特定字符的组合,组成一个“规则字符串”,这个“规则字符串”用来表达对字符串的一种过滤逻辑。 Perl 语言的正则表达式功能非常强大,很多语言设计正则式支持的时候都参考Perl的正则表达式。因此常用的表达式语法也是 Perl 兼容正则表达式。

Go 语言中使用包 regexp 提供对正则表达式的支持。本文说明 regexp 中常用的正则处理方法。

2 获取正则对象

通过编译正则表达式,可以得到正则操作对象,用于完成正则的相关处理: 函数:

  • regexp.Compile(expr string) (*Regexp, error),用于编译一个正则表达式,如果成功返回 Regexp 对象。
  • regexp.MustCompile(str string) *Regexp,与 Compile 一致,差异是失败时会宕机。

标签:

提交需求或反馈

Demand feedback