Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Serialization of a regular expression pattern

Thread Tools

Serialization of a regular expression pattern

Posts: n/a
I have a problem with the use of an object Pattern recovered from a
serialization/unserialization process.

My pattern is "([A-Z]):[A-Z])"
p = Pattern.compile ("([A-Z]):[A-Z])");
It defines 2 groups between ( ).

With the input string "B::C", the matcher normally identifies 2 groups which
values are: "B" and "C"
m = p.matcher ("B::C");

If the pattern p is serialized /unserialized before being used to create the
then the matcher identifies only 1 group, which value is "C".

Can someone explain why ???


Java version is 1.4.1_02; it runs on a Sun machine.

Here is a full test program:

import execution.TRegExpr;

import java.util.regex.*;

class a
static void find (Matcher m) {
if (m.find ()) {
int n = m.groupCount ();

for (int k=0; k<=n; k++)
System.out.println (""+k+": " (k));

public static void main (String[] args) {

String regexpr = "([A-Z]):[A-Z])";

Pattern p0 = Pattern.compile (regexpr);
Pattern p1 = null;

String fileName = "pattern.bytes";

try {
ObjectOutputStream os = new ObjectOutputStream (new
FileOutputStream (fileName));
os.writeObject (p0);
os.close ();

ObjectInputStream is = new ObjectInputStream (new
FileInputStream (fileName));
p1 = (Pattern)is.readObject ();
is.close ();
catch (Exception e) {
System.out.println ("ERROR: "+e.getMessage ());

String input = "B::C";

find (p1.matcher (input));
find (p0.matcher (input));

Reply With Quote
Roedy Green
Posts: n/a
On Tue, 22 Jul 2003 16:19:55 +0200, "MARTIN Herve"
<(E-Mail Removed)> wrote or quoted :

>Can someone explain why ???

sounds like a bug. They are not properly restoring some transient

Is there some reason you are serialising the Patterns rather than just
the pattern strings?

Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See for The Java Glossary.
Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
how to move from java object serialization to xml serialization? Dimitri Ognibene Java 4 09-02-2006 07:32 AM
Object serialization XML vs java serialization Java 3 06-14-2006 03:45 AM
Serialization Problems and books on serialization? Java 8 01-02-2005 02:40 PM
avoiding XML serialization, different WSDL generation, soap serialization Ramunas Urbonas ASP .Net Web Services 1 07-27-2004 09:57 PM
Dynamically changing the regular expression of Regular Expression validator VSK ASP .Net 2 08-24-2003 02:47 PM