問題描述
使用正則表達式處理字符串 (String Manipulation with regular expression)
I have seen in this forum, that many of the people are doing string manipulation using Regular expression. Is there any performance benefit on regex rather than the normal indexing manipulation?
It would be helpful, if you explain it how or provide any link explaining it.
‑‑‑‑‑
參考解法
方法 1:
It's not so much a performance thing; people use regular expressions because they are an easy‑to‑use yet ultra‑flexible tool for all sorts of string manipulations. Once you master the syntax, regular expressions allow you to express quite complex string manipulations, and since a regular expression itself is basically a string, you can store regular expressions in variables, files, databases, and a bunch of other things.
Consider this simple regex: /\bA[a‑z]*\b/
, which finds all whole words that start with a capital 'A', followed by only lowercase letters (e.g., running it over "And Now All Is Good" would yield ["And", "All"]). Now write a function that does the same (in the language of your choice) using only standard string manipulations. You'll quickly see the advantages of regular expressions.
方法 2:
A good amount of discussion is available on this SO question, String manipulation vs Regexps
(by NaveenBhat、tdammers、Aziz Shaikh)