Serialization of a regular expression pattern

M

MARTIN Herve

Hi,
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
matcher,
then the matcher identifies only 1 group, which value is "C".

Can someone explain why ???
Thanks,

Hervé.


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

Here is a full test program:

import execution.TRegExpr;

import java.io.*;
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+": "+m.group (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));
}
}
 
R

Roedy Green

Can someone explain why ???

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

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top