Convert regex from Java to Go

huangapple go评论80阅读模式
英文:

Convert regex from Java to Go

问题

我使用reregexp2库。

这个正则表达式在Go中不起作用,会报错:

regexp2: Compile((?<=^|[a-z]\-|[\s\p{Punct}&&[^\-]])([A-Z][A-Z0-9_]*-\d+)(?![^\W_])): error parsing regexp: unknown unicode category, script, or property 'Punct' in (?<=^|[a-z]\-|[\s\p{Punct}&&[^\-]])([A-Z][A-Z0-9_]*-\d+)(?![^\W_])

我希望它能正常执行。

英文:
(?&lt;=^|[a-z]\-|[\s\p{Punct}&amp;&amp;[^\-]])([A-Z][A-Z0-9_]*-\d+)(?![^\W_])

I use the library reregexp2

This RE does not work in Go and will report error:

regexp2: Compile(`(?&lt;=^|[a-z]\-|[\s\p{Punct}&amp;&amp;[^\-]])([A-Z][A-Z0-9_]*-\d+)(?![^\W_])`): error parsing regexp: unknown unicode category, script, or property &#39;Punct&#39; in `(?&lt;=^|[a-z]\-|[\s\p{Punct}&amp;&amp;[^\-]])([A-Z][A-Z0-9_]*-\d+)(?![^\W_])`

I hope it can be executed normally

答案1

得分: 0

如果你查看Java regex Pattern documentation,你会发现\p{Punct}表示的是标点符号:其中包括!"#$%&'()*+,-./:;<=>?@[]^_{|}~。

所以,如果你想将其转换为Go语言的正则表达式,请参考regexp syntax documentation

英文:

If you take a look at Java regex Pattern documentation, you'll see that \p{Punct} is Punctuation: One of !&quot;#$%&amp;&#39;()*+,-./:;&lt;=&gt;?@[\]^_{|}~

>\p{Punct} Punctuation: One of !"#$%&'()*+,-./:;<=>?@[]^_`{|}~

So what you need to convert this to a go regex checking the regexp syntax documentation

huangapple
  • 本文由 发表于 2023年1月4日 18:01:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75003795.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定