String Manipulation with regular expression
I have seen in this forum, that many of the peo开发者_StackOverflow社区ple 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.
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.
A good amount of discussion is available on this SO question, String manipulation vs Regexps
精彩评论