Serialization of a regular expression pattern

Discussion in 'Java' started by MARTIN Herve, Jul 22, 2003.

  1. MARTIN Herve

    MARTIN Herve Guest

    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));
    }
    }
     
    MARTIN Herve, Jul 22, 2003
    #1
    1. Advertising

  2. MARTIN Herve

    Roedy Green Guest

    On Tue, 22 Jul 2003 16:19:55 +0200, "MARTIN Herve"
    <> wrote or quoted :

    >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?

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Jul 22, 2003
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VSK
    Replies:
    2
    Views:
    2,305
  2. Replies:
    8
    Views:
    2,253
    deadsea
    Jan 2, 2005
  3. Replies:
    3
    Views:
    1,035
  4. Dimitri Ognibene
    Replies:
    4
    Views:
    785
    Dimitri Ognibene
    Sep 2, 2006
  5. Ramunas Urbonas
    Replies:
    1
    Views:
    403
    Dino Chiesa [Microsoft]
    Jul 27, 2004
Loading...

Share This Page