Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.help > #1969

Re: Regexp help

From markspace <-@.>
Newsgroups comp.lang.java.help
Subject Re: Regexp help
Date 2012-08-08 14:15 -0700
Organization A noiseless patient Spider
Message-ID <jvukt8$btq$1@dont-email.me> (permalink)
References (1 earlier) <jVfUr.51896$ls7.32117@newsfe11.iad> <e8fd1c92-6f54-48ee-8cc9-ec5a10d03ce5@googlegroups.com> <HcwUr.685$CE7.526@newsfe12.iad> <jvu6tp$gec$1@dont-email.me> <99e528d5rjt3a0ahl6bpgih7ih1gv68k26@4ax.com>

Show all headers | View raw


On 8/8/2012 12:13 PM, Gene Wirchenko wrote:
>
>       A bit of devil's advocate: I like regexes, but simple regexes.  I
> have some JavaScript code where I could have used one giant regex to
> process, but I instead chose to use simple regexes and a bit of
> processing.  It is a lot more readable than a custom parser.  The
> giant regex would have been a ball of mud though.


This is fair.  (If I had a parser generator handy, I'd use that.)  I 
agree simple regex aren't terrible.  But there's a tendency to make the 
simple regex "just a little more complicated to get over this one 
problem."  It's easy to snowball.


A simple test: two hours to hand code a parser, sans parser generator. 
Vs. 24 hours or more for the OP to figure out his regex.

I wasn't actually sure of the OP's requirements, so I just captured the 
three branch labels into three separate strings.  Do with them as you will.

run:
To: \main\2, From: \main\rel1\2, Base: \main\2)]
To: /main/4, From: /main/bugfix/1, Base: /main/2]
To: , From: , Base:
BUILD SUCCESSFUL (total time: 0 seconds)


<code>

package quicktest;

import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;

/**
  * A parser for SCC (source code control) output.
  *
  * @author Brenden
  */
public class SccParser {

    static String[] testVectors = {
    " Needs Merge \".\" [(automatic) to \\main\\2 from \\main\\rel1\\2 
(base also \\main\\2)]",
    " Needs Merge \"./update\" [to /main/4 from /main/bugfix/1 base 
/main/2]",
    " Every programmer Needs to believe in something; I believe I Needs 
another drink.",
};

    /**
     *
     * @param args
     */
    public static void main(String[] args) throws Exception {
       parse(new StringReader(testVectors[0]));
       parse(new StringReader(testVectors[1]));
       parse(new StringReader(testVectors[2]));
    }

    public static void parse(Reader reader) throws IOException {
       findMerge(reader);
    }

    // package-private
    static void findMerge(Reader reader) throws IOException {
       findString(reader, "Needs Merge");
       skipWhiteSpace(reader);
       findString(reader, "\""); // skip quoted string
       findString(reader, "\"");

       findEitherOrChar(reader, '/', '\\');
       reader.reset();                        // back one character
       String to = whiteSpaceToken( reader );
       findEitherOrChar(reader, '/', '\\');
       reader.reset();
       String from = whiteSpaceToken( reader );
       findEitherOrChar(reader, '/', '\\');
       reader.reset();
       String base = whiteSpaceToken( reader );
       System.out.println("To: "+to+", From: "+from+", Base: "+base );
    }

    // package-private
    static void findString(Reader reader, String str) throws IOException {
       if( str.length() == 0 ) return;
       outerLoop:
       for (int c; (c = reader.read()) != -1;) {
          if (c != str.charAt(0))
             continue;
          reader.mark(str.length());
          for (int i = 1, len = str.length(); i < len; i++) {
             if ((c = reader.read()) != str.charAt(i)) {
                reader.reset();
                continue outerLoop;
             }
          }
          return;
       }
    }

// etc. Remainder left as a exercise for readers trying to get us to
// do their homework/internship project.

}

Back to comp.lang.java.help | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Regexp help mike <mikaelpetterson@hotmail.com> - 2012-08-07 07:03 -0700
  Re: Regexp help Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2012-08-07 14:38 -0700
    Re: Regexp help mike <mikaelpetterson@hotmail.com> - 2012-08-07 22:14 -0700
      Re: Regexp help Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2012-08-08 09:11 -0700
        Re: Regexp help markspace <-@.> - 2012-08-08 10:16 -0700
          Re: Regexp help Gene Wirchenko <genew@ocis.net> - 2012-08-08 12:13 -0700
            Re: Regexp help markspace <-@.> - 2012-08-08 14:15 -0700
              Re: Regexp help Gene Wirchenko <genew@ocis.net> - 2012-08-08 18:43 -0700

csiph-web