views:

165

answers:

9

While I can see the value and usefulness of regular expressions, I also find that they are extremely complicated and difficult to create and debug. I am often at the point where I find their usefulness is offset by the difficulty in creating expressions.

I am a bit astonished by the fact that there is nothing quite like them and that there hasn't been an effort to recreate them use a more verbose or less arcane syntax.

so, are regular expressions here to stay? are there alternatives that are gaining traction? do other people just ignore them and write hundreds of lines of string compare functions?

+2  A: 

I am a bit astonished by the fact that there is nothing quite like them and that there hasn't been an effort to recreate them use a more verbose or less arcane syntax.

Any parser generator out there is effectively regexp "with more verbose and less arcane syntax". As well as the ability to parse more complex constructs (though some advanced regex engines can be on par with that).

For a good example of what is possible in a language with powerful enough primitives, have a look at FParsec.

Pavel Minaev
+1  A: 

Regular Expressions seem terse because they are. Each character has a lot of meaning. For an extreme example of this kind of terseness, check out this implementation of Conway's Game of Life, which only uses a couple of dozen characters in APL for its implementation.

Robert Harvey
A: 

Really depends on how good of support for string manipulation your language (and/or framework) comes with. With Ruby, I hardly even use regex even with it's excellent support; since it is so powerful with handling strings.

Pierreten
A: 

I love regular expressions. They make nearly everything I do during the day easier. From parsing and verifying user input, to greping project files for some elusive code. I could not live without them. While it is true that they are a little hard to wrap your head around initially, once you understand them a bit they really are very straight forward (with a few admittedly silly exceptions).

I think that love them or hate em, they are here to stay.

Jason Webb
A: 

I would suggest you embrace them. Virtually every language has them and they are a wonderful tool that should be a part of every programmers toolbox.

To the untrained eye, they can easily be overwhelming, but if you take the time to learn the syntax you'll find that the vast majority are usually fairly simple.

I guess they're natural to me as one of my first languages was Perl, where they really are a core part of the language.

In my opinion, a single regex is easily far more readable and maintainable than the "hundreds of lines of string compare functions" you'd suggest replacing them with.

Why has no one tried to replace them with something else? Because anything you tried to replace them with would still look just as ugly to the untrained eye. The current standard format works and is used almost everywhere, so changing it would require learning two sets of syntax to do the same job for programmers who work in multiple languages.

AllenJB
A: 

Read Jeffrey Friedl's Mastering Regular Expressions http://oreilly.com/catalog/9780596528126/ for a fuller explanation about what they really are and how they really work. They make it possible to write very powerful constructs pretty easily.

When programmers find regular expressions daunting, it's usually because they've been exposed to inefficient or poorly-written regular expressions. However, I would find it very difficult to get along without them.

David M
+6  A: 

Love and hate in all combinations.

I love how powerful regular expressions are-

I hate how hard it is to read a regular expression once you have written it.

I hate to love their usefulness. I would rather use a more managable way to do what regular expressions do, but currently it means a lot more code so it's not defendable.

I love to hate regular expressions. Bitching about them is an outlet that makes it easier to live with them.

Guffa
+1 for the last point
DrDro
A: 

Regular expressions, took me 2-3 years just to get used to them. At first, I did avoid them at all cost. Since I wanted to use Rewrite Engine in .htaccess, there is no other way around it except to use regular expression. For me, there was motivation to learn about them, so I continued to learn about them even thought I thought I won't be able to. Even to this day, I am still learning and experimenting with them. Once you get hang of them, they are a great tool for many solutions.

There are a lot of concepts in Regular expressions, but if you learn one concept at a time and experiment with it, you will eventually be able to understand them all and use them together. ([0-9a-z]*)

+1  A: 

There are always efforts underway to replace regexes or make them more user-friendly; that they haven't succeeded yet is not so surprising when you think about it. Parsing text is a difficult, messy task, and regexes are the last-resort parser--what we use when we can't assume anything about the structure of the text, or about which characters are significant and which ones aren't.

I believe the successor to regexes won't be all that much easier to learn or use, it just won't suck so bad at matching hierarchical or recursive structures. The real mystery, to me, is how a tool for matching regular languages came to be the universal default for general parsing tasks. How much of the world's information ever came in the form of text that conformed to regular grammars, even before HTML?

Alan Moore