Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disadvantages of using Regular Expressions

Recently I was advised by my manager not to depend much on Regex as it has lot of disadvantages. When I tried to learn more , I hear that it has issues like regex can result in memory leak as some objects continue to hang on strings references even after use ?

.NET RegEx "Memory Leak" investigation

So it it right to say that reg-ex causes memory overheads and should not be used if you have other options ? Is there any other disadvantaged to reg-ex (apart from it being tough to learn :) )

P.S I am developing an application (c#.net) similar to web crawler which extracts all hrefs and some other information like title, meta tags etc..I have the option of using HTML Agility pack instead of reg-ex.

like image 308
Ananth Avatar asked Oct 29 '25 07:10

Ananth


1 Answers

Makes the code difficult to read. Most of the time, even at the expense of having more verbose code, you are better off not using regular expressions. The costly performance impact and the degradation in the readability of the code means that you don't use regexes in most of the cases, especially, the simpler ones and the complex ones.

And for the purpose you are mentioning ( parsing HTML etc. ), regular expressions simple cannot get the job done ( because HTML is not a regular language ). It is is like having a hammer and everything looks like a nail.

like image 68
manojlds Avatar answered Oct 31 '25 00:10

manojlds