gpt4 book ai didi

python - 嵌套 "["的正则表达式

转载 作者:行者123 更新时间:2023-11-28 22:02:52 24 4
gpt4 key购买 nike

我熟悉正则表达式,但这个复杂的例子让我感到困惑。我试图理解这行代码在做什么:

r'/(\\.|[^[/\\\n]|\[(\\.|[^\]\\\n])*])+/([gim]+\b|\B)'

这是一个试图检测正则表达式的代码片段,例如:/\s+/。我理解它直到嵌套的 [\[( 对应物。

(我需要将此代码从 Python 移植到 Java,但我无法理解上述代码的工作原理,以及为什么它不像在 Java 中那样工作。)

最佳答案

这是一个可能有帮助的分解版本:

/                      # Match an opening slash
( # Followed by one or more...
\\. # Backslash followed by any character
| # or...
[^[/\\\n] # Something that's not a [, /, \, or newline
| # or...
\[ # A literal [, followed by any number of...
(
\\. # backslashes followed by any character
| # or...
[^\]\\\n] # something that's not a ], \, or newline
)*
] # and ending with a ]
)+
/ # And a closing slash
(
[gim]+\b # Followed by one or more of g, i, m
|
\B # or something that isn't a word boundary
)

关于python - 嵌套 "["的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10491978/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com