Anonymous - 2002-08-11

Hi Sergey

This is a small part of my comparison:
..
  /**
     Parse the source string returning Complex Number components
     in bidimensional array (real,imaginary) or null on error.

     Note: Named Group feature is exclusive JRegex implementation.
  */
  static Pattern cpxCSBRNG;
  static {
    try {
      cpxCSBRNG = new Pattern("(?# Optimal Complex Number Pattern )^(?!$)({REAL}[+-]?(?:\\d+(?:\\.\\d*)?|\\.\\d+)(?i:E[+-]?\\d+)?)?(?:({IMAGINARY}(?# STOCHASTIC BRANCH VIA BACKREFERENCE ON NAMED GROUP )(?(REAL)[+-]|[+-]?)(?:(?:\\d+(?:\\.\\d*)?|\\.\\d+)(?i:E[+-]?\\d+)?)?)i)?$");
    }
    catch (PatternSyntaxException e) {
      System.out.println(e);
    }
  }
   /**
      The target string will match pattern if typed as written i.e.: "A+Bi".
      (A,B) is a couple of floating point numbers compliant to IEEE standard
      without blanks and "i" (lowercase only) as imaginary unit symbol.
      Possible contents of capturing groups [1] to [2] are:

                 [1] [2]
                  A   •  (A,0) or A     ::  real part
                  A   B  (A,B) or A+Bi  ::  real & imaginary part
                  •   B  (0,B) or   Bi  ::  imaginary part
          where:
                  •  is null
                  A  is a floating point number
                  B  is a floating point number
                      or '-' leading to -i
                      or '+' leading to +i
                      or a zero lengthy string leading to i.

      Note: Uses Conditional Expression and BackReference as its boolean
            argument to reduce branching therefore achieving minimum
            backtracking number so is OPTIMAL.

   */

  public static double[] bestParse(String source) { /* using Named Groups */
    if (source == null) return null;
    Matcher m = cpxCSBRNG.matcher(source);
    if (!m.matches()) return null;
    final int REAL = 0, IMAGINARY = 1; /* just to ease readability */
    double complex[] = {0, 0};
    if ((source = m.group("REAL")) != null) {
      complex[REAL] = Double.parseDouble(source);
    }
    if ((source = m.group("IMAGINARY")) != null) {
      if (!source.equals("")) {
        complex[IMAGINARY] = (source.equals("-")) ? -1 /* -i */
                                 : (source.equals("+")) ? 1 /* +i */
                                       : Double.parseDouble(source);
      }
      else if (complex[REAL] == 0) complex[IMAGINARY] = 1; /* i */
    }
    return complex;
  }
..

I'll provide an download link to all source codes. Wait please. There are one source code for each package and more source codes for special cases.
Unfortunately comparison text is in Brazilian Portuguese and still there are many technical errors.

Best Regards.