用弹珠机可视化正则表达式:Regex Marble Run 让匹配过程看得见Visualize Regular Expressions With Marbles


AI free token/AI免费积分
用弹珠机可视化正则表达式:Regex Marble Run 让匹配过程看得见

如果你属于那种「看实物比看抽象概念更容易理解」的人,那么开发者 bottomless 制作的 Regex Marble Run 网站可能就是为你准备的。输入一个测试表达式和一个字符串,网站就会生成一台虚拟弹珠机,让你以物理化的方式看到正则表达式如何逐段拆解这个字符串。

以站点示例中的正则表达式为例,不熟悉正则的人初看可能觉得像天书,但逐位拆解后其实相当简单:插入符表示匹配字符串开头,方括号字符类用于匹配若干候选字符之一,方括号内的插入符则表示「取反」——匹配除列出字符之外的任意字符;星号表示前一个元素可以出现零次或多次;美元符号则把匹配锚定在字符串末尾。

看得头晕?网站的玩法是:把测试字符串的每个字母变成一颗弹珠,让它依次落过正则表达式的各个组成部分。通过匹配的弹珠最终落入左侧框,失败的落入右侧框。它是否真的能帮你学会正则不好说,但光看弹珠下雨就很有趣。

本文来源:https://hackaday.com/2026/10/20/visualize-regular-expressions-with-marbles/
若有版权问题,请来信告知:13951431913#139.com 或在右方提交评论。

If you are one of those people who visualize things physically better than in the abstract, then Regex Marble Run by bottomless might be for you. You enter a test expression and a string, and the site generates a virtual marble machine that lets you see the regular expression pick apart the string in a physical way.

Click this link, and you'll see the regular expression which, if you aren't a regular expression user, might seem a bit cryptic. However, it is fairly simple if you take it bit-by-bit.

The caret character tells it to match the start of the string. So a pattern will match at the beginning of a string but not when the target text appears in the middle. Character classes in square brackets let you match one of several alternatives, and a caret inside the brackets negates the class, matching anything except the listed characters. A quantifier such as the asterisk means the preceding element may appear zero or more times, while the dollar sign anchors the match to the end of the string.

Head spinning? The site will let you see each letter of a test string as a marble that falls through the different parts of the regular expression. Balls that pass wind up in the left-hand box, while failures are in the right-hand box. Will it help you? We don't know, but it is fun to watch.

Visualize Regular Expressions With Marbles

暂无评论,快来发表第一条评论吧!No comments yet. Be the first!

📮 需求咨询📮 Inquiry