M
Michael Powe
I experimented a bit with the Java regexp lookahead functionality, and
the results don't make sense to me. The test is below.
8<========================================>8
public static void main (String [] args)
{
// this is negative lookahead
String re = "(.*)\\[(?!\\S+)\\](.*)";
// positive lookahead
//String re = "(.*)\\[(?=\\S+)\\](.*)";
String test = "this is [sometext] and some more";
String test2 = "this is [] and some more";
Pattern p = Pattern.compile(re);
Matcher m = p.matcher(test);
if (m.find()) {
System.out.println("success match one");
for (int i = 0; i <= m.groupCount(); i++) {
System.out.println("Group " + i + " " + m.group(i));
}
} else {
System.out.println("fail match one");
}
Matcher m2 = p.matcher(test2);
if (m2.find()) {
System.out.println("success match two");
for (int i = 0; i <= m2.groupCount(); i++) {
System.out.println("Group " + i + " " + m2.group(i));
}
} else {
System.out.println("fail match two");
}
} // end main
8<========================================>8
Here's the output for positive lookahead:
cd /home/powem/src/java/
/opt/jdk1.5/bin/java DateTest
fail match one
success match two
Group 0 this is [] and some more
Group 1 this is
Group 2 and some more
And for negative lookahead:
cd /home/powem/src/java/
/opt/jdk1.5/bin/java DateTest
fail match one
fail match two
8<========================================>8
Thus, negative lookahead appears to be useless since it fails whether
the text is there or not. Positive lookahead appears to do the
opposite of what you would expect, it fails if the condition is true
(text is there) and succeeds if the condition is false.
Things that make me go "hmmm."
Am I making some fundamental error here?
Thanks.
mp
--
Michael Powe (e-mail address removed) Naugatuck CT USA
"We had pierced the veneer of outside things. We had `suffered,
starved, and triumphed, groveled down yet grasped at glory, grown
bigger in the bigness of the whole.' We had seen God in his
splendors, heard the text that Nature renders. We had reached the
naked soul of man." -- Sir Ernest Shackleton, <South>
the results don't make sense to me. The test is below.
8<========================================>8
public static void main (String [] args)
{
// this is negative lookahead
String re = "(.*)\\[(?!\\S+)\\](.*)";
// positive lookahead
//String re = "(.*)\\[(?=\\S+)\\](.*)";
String test = "this is [sometext] and some more";
String test2 = "this is [] and some more";
Pattern p = Pattern.compile(re);
Matcher m = p.matcher(test);
if (m.find()) {
System.out.println("success match one");
for (int i = 0; i <= m.groupCount(); i++) {
System.out.println("Group " + i + " " + m.group(i));
}
} else {
System.out.println("fail match one");
}
Matcher m2 = p.matcher(test2);
if (m2.find()) {
System.out.println("success match two");
for (int i = 0; i <= m2.groupCount(); i++) {
System.out.println("Group " + i + " " + m2.group(i));
}
} else {
System.out.println("fail match two");
}
} // end main
8<========================================>8
Here's the output for positive lookahead:
cd /home/powem/src/java/
/opt/jdk1.5/bin/java DateTest
fail match one
success match two
Group 0 this is [] and some more
Group 1 this is
Group 2 and some more
And for negative lookahead:
cd /home/powem/src/java/
/opt/jdk1.5/bin/java DateTest
fail match one
fail match two
8<========================================>8
Thus, negative lookahead appears to be useless since it fails whether
the text is there or not. Positive lookahead appears to do the
opposite of what you would expect, it fails if the condition is true
(text is there) and succeeds if the condition is false.
Things that make me go "hmmm."
Am I making some fundamental error here?
Thanks.
mp
--
Michael Powe (e-mail address removed) Naugatuck CT USA
"We had pierced the veneer of outside things. We had `suffered,
starved, and triumphed, groveled down yet grasped at glory, grown
bigger in the bigness of the whole.' We had seen God in his
splendors, heard the text that Nature renders. We had reached the
naked soul of man." -- Sir Ernest Shackleton, <South>