Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I was trying to parse a java properties file to avoid some Properties loading pitfalls (non ordering, loss of comments).

(Property are of the form 'key = value' where the value can span on multiple lines using the \ special char)

I have tried to get the right regexp to parse the properties but after I gave up after fighting for more than one hour. (a version for groovy found here:
~ /^([^#=].*?)=(.+)$/)

I manage to write the same thing using a single loop and very trivial code in 5 minutes...

I'm an average programmer when dealing with regexp: I have to re-read the doc each time I want to use them and advanced feature are still obscure for me.

Often when dealing with regexp I have the feeling that some very skilled developers can produce/decipher very elaborated regexp in seconds, but others have trouble to handle them.

Question: should I take time to deeply learn regexp and produce cryptic and powerful parser, or should I keep using some ugly easy to debug/understand parsing algo ?

share|improve this question

2 Answers 2

up vote 2 down vote accepted

For this specific task (parsing java properties file), first look for an existing solution (sounds like someone would have solved this same problem already), and if you can't find one, it's not necessarily bad to create a non-regex parser to do it with - you use the tools you know.

If you do go the regex route, do not be compelled to use a single regex, when two (or more) will often give a much simpler and faster result.

should I take time to deeply learn regexp

YES!

There are a lot of times when knowing regular expressions can greatly speed up your programming - I probably use more regex in the tasks of creating & maintaining code than in actuall code itself.

But perhaps the main reasons for learning regex deeply is so that you can quickly say "this is a job for regex" or "regex is the wrong tool here" - otherwise you can waste a lot of time trying to get regex to do a task it just isn't suited for.

However, it's important to also state that Regex does not need to be crypic - you can write complex regex and format it nicely so that it is just as understandable as any other code.

share|improve this answer
    
Thanks for the link... very interesting material ! –  Guillaume Nov 9 '10 at 10:40

I'm like you, I use regexps so rarely that I have to look in a reference for all but the most trivial examples. I've worked with a couple of developers who were pretty fluent with them though and they could pretty surprising things very quickly. I think it is something that once you know more about it, you will see more and more ways to use it and the investment will pay off. Also it looks like a good way to stretch your mind.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.