Composed Regular Expressions - breaking a regex down into a readable form

后端 未结 3 1429
不思量自难忘°
不思量自难忘° 2021-01-26 07:39

I was reading an article put together by Martin Fowler regarding Composed Regular Expressions. This is where you might take code such as this:

const string patt         


        
3条回答
  •  再見小時候
    2021-01-26 08:21

    Yes, absolutely. Regexes are powerful, but because of their terse syntax, extremely unreadable. When I read a comment such as "this matches an URI", that doesn't actually help me figure out how it does that, and where I should look to (for example) fix a bug where it doesn't match some obscure corner case in query string properly. Regex is code; document it as you'd document a function. If it's short and (reasonably) clear, a single comment for the entire regex is fine. If it's complicated, clearly highlight and comment individual parts. If it's really complex, split it into several regexes.

提交回复
热议问题