0

Can someone list comprehensive list the pros and cons of using regular expressions in Java programing?

MatBanik
  • 24,206
  • 38
  • 107
  • 172
  • 1
    In which area are you looking for pros and cons? RegExes in Java vs. other languages, RegExes vs. manual string parsing, something else? – dlev May 13 '11 at 00:09
  • 3
    "Some people, when confronted with a problem, think “I know, I'll use regular expressions.” Now they have two problems." - Jamie Zawinski – JasCav May 13 '11 at 00:11
  • 2
    [Already answered here.](http://stackoverflow.com/questions/5767627/java-regex-helper/5771326#5771326) – tchrist May 13 '11 at 00:45

3 Answers3

3

Pro: when regular expressions do what you need.

Con: when they don't.

Other than that, the question as stated is mostly ideological.

Ted Hopp
  • 222,293
  • 47
  • 371
  • 489
0

Pros: They are an effective way to match against input. They are easily configurable and can be separated from code.

Cons: They be hard to read. They are not performant. If performance is a concern do not use them.

ditkin
  • 6,046
  • 1
  • 28
  • 36
  • Perhaps "do not always perform well" is more accurate? – Mitch Wheat May 13 '11 at 00:19
  • @mitch-wheat It is very hard to reason about their cost and more often then not they will be the culprit in non-performant code. There are lots of times you don't need to care about performance, but if you need to care I recommend avoiding regular expressions. – ditkin May 13 '11 at 00:43
  • What the devil kind of weasel word is “performant”?? – tchrist May 13 '11 at 00:45
  • Regexes are only as slow as the programmer who writes them makes them. – tchrist May 13 '11 at 00:46
  • If they are hard to read, it is the fault their writer. They should always be easy to read: that’s what `Pattern.COMMENTS` or `(?x)` is all about. The most important thing about that mode is that you can group things logically with indentation and whitespace. But for a real barnstormer, you need to use [grammatical patterns](http://stackoverflow.com/questions/4840988/the-recognizing-power-of-modern-regexes/4843579#4843579). – tchrist May 13 '11 at 00:50
-4

Pro: It works and it's simple.

Con: There are none.

Why ask? Perhaps you have something more specific you'd like to know?

S.Lott
  • 359,791
  • 75
  • 487
  • 757
  • 1
    Cons: Complex Regex's can be hard to understand, debug and maintain – Mitch Wheat May 13 '11 at 00:11
  • 1
    @Mitch Wheat: **All** code can be hard to understand, debug and maintain. A regular expression can be much easier to understand, debug and maintain than the equivalent program logic written out "the hard way". – S.Lott May 13 '11 at 00:14
  • @Mitch,@S.Lott: and might also be heavy to compute. also they are none-exhaustive. consider you are looking for a* in the string aaaaa. you will not get all results (which may be what you want, or not.. depending on situation) – amit May 13 '11 at 00:14
  • @S.Lott: while that's true, regex's have a tendency to be hard to understand (and that's even without backtracking!). I'm sure everyone has seen regex's used to solve problems where a simpler solution was more appropriate. Not my downvote BTW – Mitch Wheat May 13 '11 at 00:17
  • @Mitch Wheat: All code has a tendency to be hard to understand. I'm sure everyone has seen bad code used to solve problems where simpler code was appropriate. Regex's are no different than any other programming language. – S.Lott May 13 '11 at 00:20
  • 1
    @S.Lott: In theory Yes. In practice, I think it's hard to justify your assertion that regex's are simple. – Mitch Wheat May 13 '11 at 00:21
  • @Mitch Wheat: In theory and in practice it's just another language. No different than all other programming languages. Except, of course, it has profound limitations. AFAIK, Regular Expressions aren't Turing Complete. But they're just a language. Nothing more. – S.Lott May 13 '11 at 00:25
  • 1
    -1, they are not simple. "In theory and in practice it's just another language." Exactly! In order to use them you need to learn another programming language. It makes it flexible and powerful but not "simple". – camickr May 13 '11 at 00:27
  • @camickr: Suddenly one more programming language is too complex? What's the correct number of programming languages? And why is just the regular expression language singled out? What about CSS or HTML? Aren't they "another programming language" and doesn't using CSS make things too complex? I'm unclear on why this (and only this) language is singled out. – S.Lott May 13 '11 at 00:30
  • See [this answer](http://stackoverflow.com/questions/4840988/the-recognizing-power-of-modern-regexes/4843579#4843579) and [perhaps some of these](http://stackoverflow.com/users/471272/tchrist#qpage_1-anpage_1-qsort_votes-ansort_votes) for how to write readable, maintainable, and powerful regexes. – tchrist May 13 '11 at 00:52
  • Yep, regex is so simple... see the conversations in this question: http://stackoverflow.com/questions/5986392/how-to-match-this-date-format-with-a-regex. Even the people answering the question are asking one another for help and clarification. – camickr May 13 '11 at 02:28