rjray writes "Over at
The
example program takes 92 lines to implement a file grep. It implements the pattern
Re:Not very Perl-like....
Ovid on 2002-02-15T00:59:17
Hmmm... now I'm rather curious as to what's going on here. Here's the regex code to match a line:
// Pattern used to parse lines
private static Pattern linePattern
= Pattern.compile(".*\r?\n");
I don't think VSarkiss' criticism of the dot star is appropriate in this case as Java documentation states that the dot does not match a line terminator. However, they appear to have goofed up the line terminator! What about \r on Macs? From what I can tell from their docs, the carriage return/newline in the regex is superfluous. They could at least get their regexes straight if they post sample code. They have multiline mode which appears to be what they were actually looking for and would be more robust than the listed solution.
Re:Not very Perl-like....
Desmodromic on 2002-02-15T05:58:23
Argh. Don't have to convince me that Java is a big fat slug, but let's be fair: The first 43 lines of that are comments. A far cry from Perl, but only half as far... Re:Not very Perl-like....
VSarkiss on 2002-02-15T21:19:40
I only counted the code, not the comments. With the comments it's 135 lines.
Re:Severely b0rken
manu4ever on 2002-02-15T13:40:58
This was fixed between beta 2 and beta 3. eg:import java.util.regex.*;
public class Regexp { public static void main(String[] args) { Pattern p = Pattern.compile("="); String[] theStrings = p.split("foo=bar=20", 2);
for (int i=0; itheStrings.length; i++) { System.out.println(i + " : " + theStrings[i]); } } }
Gives
0 : foo 1 : bar=20
as expected on 1.4.0 beta3 (and final release), but
0 : foo 1 : bar
on the beta 2.Re:Severely b0rken
jdavidb on 2002-02-15T18:48:18
I don't understand your example. split(/=/, "foo=bar=20", 2) I presume the "2" means "only give me two return values," because you say we'd expect to get two return values. Without the 2 I'd expect to get a list of {foo bar 2}, with the 2 I'd expect to get what you say I wouldn't expect: {foo bar}. Huh?
Never mind...
jdavidb on 2002-02-15T18:49:53
Oh, wow! I just learned something about Perl.
I've rarely used the final parameter to split, so I didn't know.
Re:Never mind...
Matts on 2002-02-15T18:57:14
That final parameter is *incredibly* useful. Imagine parsing email headers, or cookies, or config files, or... the list goes on. Glad to hear they fixed it though.
Perl has other features besides Regexps, which I like very much. Stuff like nested data-structures, dynamic typing, functions as first-order values, closures, eval, multiple-inheritance, etc. etc.
I don't think Java has all that, or should. That's why I still prefer Perl for most purposes.