我不明白的正则表达式

时间:2012-01-08 15:06:08

标签: java regex

我正在努力理解这个正则表达式,你能帮助我吗?

(?s)\\{\\{wotd\\|(.+?)\\|(.+?)\\|([^#\\|]+).*?\\}\\}
  • 我真的不明白DOTALL的含义:(?s)
  • 为什么在\\之前加倍}
  • 这究竟意味着什么:(.+?)(我们应该这样说:.,然后+代理.,然后?回复到.+的结果?

1 个答案:

答案 0 :(得分:8)

这个正则表达式来自一个字符串。 “规范”正则表达式是:

(?s)\{\{wotd\|(.+?)\|(.+?)\|([^#\|]+).*?\}\}

DOTALL修饰符意味着点也可以匹配换行符,但补充字符类也是如此,至少使用Java:即[^a]将匹配每个不是a的字符,包括换行符。一些正则表达式引擎与补充字符类中的换行符不匹配(这可以被视为错误)。

+?*?是惰性量词(通常应避免使用)。这意味着他们必须在他们想要吞下的每个角色之前向前看,看看这个角色是否能够满足正则表达式的下一个组成部分。

{}前面有\的事实是因为{...}是重复量词{n,m},其中n和m是整数。

此外,转义字符类|中的管[^#\|]无用,它可以简单地写为[^#|]

最后,.*?最后似乎吞下了剩下的田地。更好的选择是使用normal* (special normal*)*模式,normal[^|}]special\|

这是正则表达式,不使用延迟量词,“固定”字符类和修改后的结尾。请注意,DOTALL修改器也已消失,因为不再使用该点:

\{\{wotd\|([^|]+)\|([^|]+)\|([^#|]+)[^|}]*(?:\|[^|}]*)*\}\}

一步一步:

\{\{         # literal "{{", followed by
wotd         # literal "wotd", followed by
\|           # literal "|", followed by
([^|]+)      # one or more characters which are not a "|" (captured), followed by
\|           # literal "|", followed by
([^|]+)      # one or more characters which are not a "|" (captured), followed by
\|           # literal "|", followed by
([^#|]+)     # one or more characters which are not "|" or "#", followed by
[^|}]*       # zero or more characters which are not "|" or "}", followed by
(?:          # begin group
  \|         # a literal "|", followed by
  [^|}]*     # zero or more characters which are not "|" or "}"
)            # end group
*            # zero or more times, followed by
\}\}         # literal "}}"
相关问题