我可以使用 Go Regexp 查找除某个字符串之外的所有内容吗?
正如许多类似的问题所示,使用 Go 匹配特定字符串正则表达式可能具有挑战性。此问题询问如何匹配格式为“anything/anything/somestring”的字符串,但排除包含“somestring”的字符串。
模式“/././(。 )”将匹配“somestring”,但我们希望匹配除它之外的所有内容。大多数建议的解决方案使用“/././((?!somestring).)”,这在 Go 正则表达式中无效,因为它包含不可量化的标记。
Go 的正则表达式模块不支持前瞻来确保线性执行时间 (O(n)) 限制。因此,解决方法是必要的。
否定模式的 Web 服务
一种方法是使用非匹配正则表达式 Web 服务来生成与 POSIX 兼容的否定模式。对于“somestring”,生成的模式是:
^(1|s(s|o(s|m(s|es(omes))(s| t(s|r(s|i(s|ns)))|o(s|ms)))))(2|o( 3|m(4|e(1|s(omes)(5|t(6|r(7|i( 8|n9)))|o(3|m(4|e1)))))))(s(s|o(s|m(s|es(哦mes)(s|t(s|r(s|i(s|ns)))|o(s|ms)))))(o((me?)?|mes(omes) )*(t(r?|rin?)|o(我?)?)?))?)?$
替换最后一个“(.*)”和“”在你原来的正则表达式中的产量:
/10/10/((1 |s(s|o(s|m(s|es(omes))(s|t(s|r(s|i(s|ns)))|o(s|ms)))) )(2|o(3|m(4 |e(1|s(奥姆斯)(5|t(6| r(7|i(8|n9)))|o( 3|m(4|e1))))))))( s(s|o (s|m(s|es(omes)(s|t(s|r(s|i(s|ns)))|o(s|ms)))))(o( (我?)?|mes(omes)*(t(r?|rin?)|o(我?)?)?))?)?)$
这个确保正则表达式仅捕获第三个反斜杠之后的部分。
捕获所有三个部分
替代解决方案捕获您感兴趣的所有三个部分。检查后捕获组 #1 值,您可以决定返回什么:
package main import ( "fmt" "regexp" ) func main() { s := "anything/anything/somestring" r := regexp.MustCompile(`^[^/]+/[^/]+/(.*)`) val := r.FindStringSubmatch(s) // fmt.Println(val[1]) // -> somestring if len(val) > 1 && val[1] != "somestring" { // val has more than 1 element and is not equal to somestring? fmt.Println(val[1]) // Use val[1] } else { fmt.Println("No match") // Else, report no match } }
以上是如何使用 Go Regexp 匹配除特定字符串之外的所有内容?的详细内容。更多信息请关注PHP中文网其他相关文章!

whentestinggocodewithinitfunctions,useexplicitseTupfunctionsorseParateTestFileSteSteTepteTementDippedDependendendencyOnInItfunctionsIdeFunctionSideFunctionsEffect.1)useexplicitsetupfunctionStocontrolglobalvaribalization.2)createSepEpontrolglobalvarialization

go'serrorhandlingurturnserrorsasvalues,与Javaandpythonwhichuseexceptions.1)go'smethodensursexplitirorhanderling,propertingrobustcodebutincreasingverbosity.2)

AnefactiveInterfaceoisminimal,clear and promotesloosecoupling.1)minimizeTheInterfaceForflexibility andeaseofimplementation.2)useInterInterfaceForeabStractionTosWapImplementations withCallingCallingCode.3)

集中式错误处理在Go语言中可以提升代码的可读性和可维护性。其实现方式和优势包括:1.将错误处理逻辑从业务逻辑中分离,简化代码。2.通过集中处理错误,确保错误处理的一致性。3.使用defer和recover来捕获和处理panic,增强程序健壮性。

Ingo,替代词Inivuntionsionializatializatializationfunctionsandsingletons.1)customInitializationfunctions hallowexpliticpliticpliticconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconcontirization curssementializatizatupsetups.2)单次固定元素限制ininconinconcurrent

Gohandlesinterfacesandtypeassertionseffectively,enhancingcodeflexibilityandrobustness.1)Typeassertionsallowruntimetypechecking,asseenwiththeShapeinterfaceandCircletype.2)Typeswitcheshandlemultipletypesefficiently,usefulforvariousshapesimplementingthe

Go语言的错误处理通过errors.Is和errors.As函数变得更加灵活和可读。1.errors.Is用于检查错误是否与指定错误相同,适用于错误链的处理。2.errors.As不仅能检查错误类型,还能将错误转换为具体类型,方便提取错误信息。使用这些函数可以简化错误处理逻辑,但需注意错误链的正确传递和避免过度依赖以防代码复杂化。

tomakegoapplicationsRunfasterandMorefly,useProflingTools,leverageConCurrency,andManageMoryfectily.1)usepprofforcpuorforcpuandmemoryproflingtoidentifybottlenecks.2)upitizegorizegoroutizegoroutinesandchannelstoparalletaparelalyizetasksandimproverperformance.3)


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

mPDF
mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

禅工作室 13.0.1
功能强大的PHP集成开发环境

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

SublimeText3 Linux新版
SublimeText3 Linux最新版

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具