Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Not as good as in Ruby?  RSS feed

 
Volker Schnitzer
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I haven't had the chance yet to get more familiar with regular expressions. I have heard the complaint that regular expressions in Jave are not as integrated into the language and as convenient to use as in Ruby. Is that complaint justified, and if so, is Jave trying to catch up and make the use of regular expressions more user-friendly?
 
chi Lin
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know Ruby, but I do have some experience in Perl.
To me, Java-Regex is easier to understand & use than Perl-Regex.
my 2cs
---------------
not so smart guy still curious to learn new stuff every now and then
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is true that regex's in Java are not as integrated into the language as in Perl for example. While in Perl you can embed a regular expression as-is for an if-statement's condition, in Java you have to encapsulate the regular expression into a String object and compile it into a Pattern & Matcher object before executing the regular expression matching logic.
I doubt that we'll see regular expressions being any more integrated to the Java language than this.
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unless Sun decides to change the entire behaviour of the String class to integrate regular expressions into it AND changed the if statement to allow for a regular expression as a condition you'll never get the complete integration Perl and Ruby (and Python) offer.
As Java is not a language intended primarilly for text processing (as Perl was intended to) that's not a bad thing.
As it is only a few extra steps are needed and those are quite simple ones and more thoroughly (IMO) define REs as the Objects they are independent of String.
 
Max Habibi
town drunk
( and author)
Sheriff
Posts: 4118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Unless Sun decides to change the entire behavior of the String class to integrate regular expressions into it AND changed the if statement to allow for a regular expression as a condition you'll never get the complete integration Perl and Ruby (and Python) offer.

Actually, this has already been done to some degree in jdk 1.4: check out the CharSequence interface, which String now implements. Also, check out the matches, replaceFirst, and replaceAll methods.
In general, I think the real difference between Java regex and Perl isn't so much one of capacity, but one of mindset. For example, in Perl, you have conditional constructs. however, in Java, you have rich conditional logic.
Is one better then the other? I don't think so, so long as you don't let learning interference keep you from picking up a new skill. I actually make quite the big deal about 'Java oriented regex' in my book.
All best,
M
[ April 14, 2004: Message edited by: Max Habibi ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!