A Visual Approach to Learn Regular Expression - 用視覺的方式學(xué)習(xí)正則表達(dá)式

Like many others, understanding regular expression was one of the most difficult things I ever had to do. I had to because as an iOS developer (an UI builder or sometimes just a button writer), regular expression is inevitable in cases like, say, you wanna make sure the user input you get is a valid email address. As a Google Oriented Programmer, I tried to search for helpful materials from the web, but what I got for "regular expression" was boring and tedious. Reading a long reference book before actually try a reg-ex pattern was frustrating. I was desperate for some quick examples to help me see the forest from the trees.

像許多人一樣,理解正則表達(dá)式的概念曾對(duì)我是一個(gè)大難題。作為一個(gè)iOS開發(fā)(UI開發(fā)或者寫button的),正則在某些場(chǎng)景是必須要用的。比如,你想確定一個(gè)用戶輸入的內(nèi)容是合法的email地址。同時(shí),作為一個(gè)面向Google編程的開發(fā)者,我嘗試從網(wǎng)上搜索學(xué)習(xí)正則的內(nèi)容,但它們大多都是枯燥無聊的。要使用正則之前還得讀很多材料,這種太難受了。當(dāng)時(shí)的我非常需要幾個(gè)快速的例子,讓我找到一些對(duì)正則的感覺。

Recently, inspired by the Dragon Book, I decided to invent a new wheel which can visually present how a regular expression matching is actually done. Thus the project RegSwift.

最近,受龍書啟發(fā),我開發(fā)了一個(gè)工具來用視覺的方式展示正則匹配的過程。 RegSwift.

RegSwift parses your input reg-ex patterns and internally creates an NFA. It then uses Graphviz to render the equivalent directed graph to an image. As you click the 'Evolve' button, nodes in orange would transit along the arrows to hint state transitions. Every time a 'fin'-prefixed state is hit, it indicates a match is found.

RegSwfit將你輸入的pattern解析成NFA。然后使用Graphviz將NFA繪制成相應(yīng)的圖片。之后每次點(diǎn)擊Evolve按鈕,狀態(tài)節(jié)點(diǎn)都會(huì)沿箭頭方向演進(jìn),展示狀態(tài)轉(zhuǎn)換的過程。每次有fin標(biāo)識(shí)的狀態(tài)被觸達(dá),則說明找到了一個(gè)匹配。

RegSwift is an interactive tool, which shows you how NFAs and regular expressions work. It cost me several weekends so please star it if you like it, I really appreciate it. I don't mind if you star it more than once you have more github accounts. Many thanks!

RegSwift是一個(gè)交互式的工具,它用視覺的方式告訴你NFA和正則的原理。我花了幾個(gè)周末來開發(fā)這個(gè)工具。所以如果你喜歡這個(gè)repo,請(qǐng)start它。如果你有好幾個(gè)github賬號(hào),都star一下我也不介意~ 多謝!

https://github.com/White-White/RegSwift

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容