寻求正则表达式语法图工具或网站

MrD*_*rD. 4 regex

我对阅读正则表达式还很陌生,但发现很难阅读。我记得在找到一个网站之前会将正则表达式转换为语法图。不过,我已经尝试了好几次才能再次找到它。所以我越来越担心我可能只是想像得到的。

我想知道是否有人知道能够将正则表达式转换为语法图的工具或网站?

谢谢。

gfu*_*lam 5

Regexper

Regexper is the base from which @ForbesLindesay's Regexplained is forked and so deserves a mention here.

It is worth pointing out that (as of this writing) the two syntax diagram generators differ slightly, yet perhaps significantly in their output. Where Regexper seems to be more terse, Regexplained is — in my opinion — verbose to the extent of being at least redundant and at worst misleading. No disrespect is intended.

An example: A comparison of the following regular expression which matches repeating or non-single digit values from 1–7 separated by commas.

([1-7]{1}),(?:[1-7]{1},)*(?:\1|[^1-7,]|(?:[1-7]{2,}))

Syntax diagram via regexper.com
通过regexper.com的语法图

Syntax diagram via regexplained.co.uk
在此处输入图片说明

In the latter, notice how the path for the expression [1-7]{1} loops around with the label "1 times".

At best, this is redundant, but at worst it can mislead the reader to believe that the expression must be matched twice, because the expression must be read through one time before the reader is technically allowed to follow the looped path (see: "Quantifiers" in "Reading Railroad Diagrams").

Here it is implied that you should loop "1 times" after reading the expression.

This notation is apparent in the expressions (?:[1-7]{1},)* and (?:[1-7]{2,}) as well.

At first glance, Regexplained syntax feels right, especially for an expression like (?:[1-7]{2,}), where it initially makes sense to see "2+ times" output for {2,}. In fact, I preferred this notation for a time.

But technically, an expression is read once before the loop path. So the loop should only occur "1+ times" as is correctly indicated in the Regexper syntax diagram.

For this reason, I prefer and recommend Regexper.