This is the mail archive of the
mauve-patches@sources.redhat.com
mailing list for the Mauve project.
java.util.regex.Matcher new test for group
- From: Stephen Compall <s11 at member dot fsf dot org>
- To: mauve-patches at sources dot redhat dot com
- Date: 12 Dec 2004 19:55:44 -0600
- Subject: java.util.regex.Matcher new test for group
This new test clarifies the difference between a matching, empty
group, such as "(q*)" in "hello", who should return "" on group(1),
and a non-matching, optional group, such as "(?:(.+?)@)?" in
"ftp.gnu.org", who should return null on group(1).
2004-12-12 Stephen Compall <s11@member.fsf.org>
* gnu/testlet/java/util/regex/OptionalGroups.java: New test.
--- mauve.orig/gnu/testlet/java/util/regex/OptionalGroups.java 1969-12-31 18:00:00.000000000 -0600
+++ mauve/gnu/testlet/java/util/regex/OptionalGroups.java 2004-12-12 19:43:54.301798678 -0600
@@ -0,0 +1,52 @@
+// Tags: JDK1.4
+
+// Copyright (C) 2004 Stephen Compall
+
+// This file is part of Mauve.
+
+// Mauve is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 2, or (at your option)
+// any later version.
+
+// Mauve is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License
+// along with Mauve; see the file COPYING. If not, write to
+// the Free Software Foundation, 59 Temple Place - Suite 330,
+// Boston, MA 02111-1307, USA.
+
+package gnu.testlet.java.util.regex;
+
+import gnu.testlet.*;
+import java.util.regex.*;
+
+/**
+ * Tests optional match groups in matchers.
+ */
+public class OptionalGroups implements Testlet
+{
+ public void test (TestHarness harness)
+ {
+ Pattern opt_regex = Pattern.compile ("'(qqq)?");
+ Matcher match = opt_regex.matcher ("the world needs more 'q's");
+ harness.check (match.find (), true);
+ harness.check (match.group (1), null);
+ harness.check (match.group (), "'");
+
+ match = opt_regex.matcher ("here are some: qqqqqqq");
+ harness.check (match.find (), false);
+
+ match = opt_regex.matcher ("What? try these instead: 'qqq");
+ harness.check (match.find (), true);
+ harness.check (match.group (1), "qqq");
+
+ opt_regex = Pattern.compile ("'((?:qqq)?)");
+ match = opt_regex.matcher ("a ' is a quote");
+ harness.check (match.find (), true);
+ harness.check (match.group (1), "");
+ }
+}
--
Stephen Compall or s11 or sirian
He who hates vices hates mankind.
Majic Medco Perl-RSA Marxist IRA mindwar MILSATCOM Albright president
IMF Janet Reno Verisign bomb airframe bluebird