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


Groups > comp.lang.java.programmer > #13239

Re: Keeping the split token in a Java regular expression

From Martin Gregorie <martin@address-in-sig.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: Keeping the split token in a Java regular expression
Date 2012-03-27 21:57 +0000
Organization UK Free Software Network
Message-ID <jktd4e$kef$1@localhost.localdomain> (permalink)
References (1 earlier) <9569964.403.1332796867513.JavaMail.geo-discussion-forums@ynne2> <jkqs7e$jek$1@dont-email.me> <jkqsi1$m3l$1@dont-email.me> <jkr1tf$iql$1@dont-email.me> <jkr4f6$sf1$1@localhost.localdomain>

Show all headers | View raw


On Tue, 27 Mar 2012 01:17:26 +0000, Martin Gregorie wrote:

>    Its rather late here, so I'll leave this as an exercise for anybody
>    who feels keen. If nobody has touched it by mid morning tomorrow I
>    may see if it works.
>
I put together the following this morning. Hopefully its enough of an SSCE 
to pass muster. 

As promised, I first implemented a two-pass splitter (the 'classico' 
method): its ugly all right, even though it does the trick.

Then I swiped Stefan's code (the 'patternista' method), tewaked it 
slightly and used it to drive both his and my regexes. The only other 
changed it needs is to parameterise Matcher.group() because Stefan's regex 
treats the whole pattern as a capture group while mine only uses the 
first capture group in the pattern which lets it discard the comma 
separators. This was one of my design aims: to output the exact same 
strings as the classico() method does.

==========================================================================
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Splitter
{
   public static ArrayList<String> classico(String in)
   {
      String[] sList = in.split("PM, +|PM");
      for (int i=0; i<sList.length; i++)
         sList[i] = sList[i].trim() + " PM";

      ArrayList<String> aList = new ArrayList<String>();
      for (String s : sList)
      {
         String sp[] = s.split("AM, +|AM");
         for (int j=0; j < sp.length - 1; j++)
            aList.add(sp[j].trim() + " AM");

         aList.add(sp[sp.length - 1]);  // The last element is 
                                        // always ended wth PM
      }

      return aList;
   }

   public static ArrayList<String> patternista(String p, int g, String in)
   {
      Pattern pattern = Pattern.compile(p, Pattern.CASE_INSENSITIVE);
      Matcher matcher = pattern.matcher(in);
      ArrayList<String> aList = new ArrayList<String>();
      while(matcher.find())
      {
         String s = matcher.group(g);
         aList.add(s.trim());
      }

      return aList;
   }

   public static void showResult(String source,
                                 String method,
                                 ArrayList<String> s)
   {
      System.out.println(String.format("\n'%s' ==> '%s'", 
                                       source, 
                                       method));
      for (int i = 0; i < s.size(); i++)
         System.out.println(String.format("%2d: %s", i, s.get(i)));
   }

   public static void main(String[] args)
   {
      String SOURCE = "Fri 7:30 PM, Sat 1, 3 and 5 AM, Sun 2:30 PM";
      String martin = "(.*?[AP]M),?";
      String stefan = ".*?(?:am|pm),?";
      
      ArrayList<String> s;
      s = classico(SOURCE);
      showResult(SOURCE, "classico", s);
      s = patternista(martin, 1, SOURCE);
      showResult(SOURCE, martin, s);
      s = patternista(stefan, 0, SOURCE);
      showResult(SOURCE, stefan, s);
   }
}
==========================================================================
'Fri 7:30 PM, Sat 1, 3 and 5 AM, Sun 2:30 PM' ==> 'classico'
 0: Fri 7:30 PM
 1: Sat 1, 3 and 5 AM
 2: Sun 2:30 PM

'Fri 7:30 PM, Sat 1, 3 and 5 AM, Sun 2:30 PM' ==> '(.*?[AP]M),?'
 0: Fri 7:30 PM
 1: Sat 1, 3 and 5 AM
 2: Sun 2:30 PM

'Fri 7:30 PM, Sat 1, 3 and 5 AM, Sun 2:30 PM' ==> '.*?(?:am|pm),?'
 0: Fri 7:30 PM,
 1: Sat 1, 3 and 5 AM,
 2: Sun 2:30 PM
==========================================================================

As you can see, once I'd swapped greedy matches for non-greedy in my regex 
(the second test run), both regexes do job and to my mind use much more 
elegant code than the two pass classico approach, but of course ymmv.  


-- 
martin@   | Martin Gregorie
gregorie. | Essex, UK
org       |

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


Thread

Keeping the split token in a Java regular expression laredotornado <laredotornado@zipmail.com> - 2012-03-26 11:54 -0700
  Re: Keeping the split token in a Java regular expression Lew <lewbloch@gmail.com> - 2012-03-26 12:22 -0700
    Re: Keeping the split token in a Java regular expression Robert Klemme <shortcutter@googlemail.com> - 2012-03-26 22:01 +0200
      Re: Keeping the split token in a Java regular expression Arne Vajhøj <arne@vajhoej.dk> - 2012-03-26 21:46 -0400
        Re: Keeping the split token in a Java regular expression Robert Klemme <shortcutter@googlemail.com> - 2012-03-27 23:01 +0200
          Re: Keeping the split token in a Java regular expression Arne Vajhøj <arne@vajhoej.dk> - 2012-03-27 17:18 -0400
          Re: Keeping the split token in a Java regular expression Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2012-03-27 14:21 -0700
            Re: Keeping the split token in a Java regular expression Robert Klemme <shortcutter@googlemail.com> - 2012-03-28 07:38 +0200
              Re: Keeping the split token in a Java regular expression Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2012-03-28 10:24 -0700
  Re: Keeping the split token in a Java regular expression markspace <-@.> - 2012-03-26 13:49 -0700
  Re: Keeping the split token in a Java regular expression laredotornado@gmail.com - 2012-03-26 14:21 -0700
    Re: Keeping the split token in a Java regular expression markspace <-@.> - 2012-03-26 15:02 -0700
    Re: Keeping the split token in a Java regular expression Knute Johnson <nospam@knutejohnson.com> - 2012-03-26 15:56 -0700
      Re: Keeping the split token in a Java regular expression markspace <-@.> - 2012-03-26 16:02 -0700
        Re: Keeping the split token in a Java regular expression Knute Johnson <nospam@knutejohnson.com> - 2012-03-26 17:33 -0700
          Re: Keeping the split token in a Java regular expression Martin Gregorie <martin@address-in-sig.invalid> - 2012-03-27 01:17 +0000
            Re: Keeping the split token in a Java regular expression Martin Gregorie <martin@address-in-sig.invalid> - 2012-03-27 21:57 +0000
    Re: Keeping the split token in a Java regular expression Gene Wirchenko <genew@ocis.net> - 2012-03-26 18:26 -0700
      Re: Keeping the split token in a Java regular expression Lew <lewbloch@gmail.com> - 2012-03-26 19:07 -0700
        Re: Keeping the split token in a Java regular expression Knute Johnson <nospam@knutejohnson.com> - 2012-03-26 20:40 -0700
          Re: Keeping the split token in a Java regular expression Gene Wirchenko <genew@ocis.net> - 2012-03-27 09:10 -0700
            Re: Keeping the split token in a Java regular expression Lew <lewbloch@gmail.com> - 2012-03-27 11:09 -0700
              Re: Keeping the split token in a Java regular expression Gene Wirchenko <genew@ocis.net> - 2012-03-27 13:32 -0700
                Re: Keeping the split token in a Java regular expression Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2012-03-27 14:29 -0700
                Re: Keeping the split token in a Java regular expression Gene Wirchenko <genew@ocis.net> - 2012-03-27 16:22 -0700
                Re: Keeping the split token in a Java regular expression Gene Wirchenko <genew@ocis.net> - 2012-03-27 18:20 -0700
                Re: Keeping the split token in a Java regular expression Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2012-03-27 18:27 -0700
                Re: Keeping the split token in a Java regular expression Gene Wirchenko <genew@ocis.net> - 2012-03-27 21:31 -0700
                Re: Keeping the split token in a Java regular expression Robert Klemme <shortcutter@googlemail.com> - 2012-03-28 07:41 +0200
                Re: Keeping the split token in a Java regular expression Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2012-03-28 10:28 -0700
  Re: Keeping the split token in a Java regular expression Lew <lewbloch@gmail.com> - 2012-03-26 16:26 -0700
    Re: Keeping the split token in a Java regular expression Knute Johnson <nospam@knutejohnson.com> - 2012-03-26 17:36 -0700
    Re: Keeping the split token in a Java regular expression Robert Klemme <shortcutter@googlemail.com> - 2012-03-27 23:27 +0200
      Re: Keeping the split token in a Java regular expression Robert Klemme <shortcutter@googlemail.com> - 2012-03-28 07:28 +0200
  Re: Keeping the split token in a Java regular expression "John B. Matthews" <nospam@nospam.invalid> - 2012-03-26 20:49 -0400
  Re: Keeping the split token in a Java regular expression Arne Vajhøj <arne@vajhoej.dk> - 2012-03-26 21:58 -0400
    Re: Keeping the split token in a Java regular expression Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2012-03-26 21:14 -0700
      Re: Keeping the split token in a Java regular expression Arne Vajhøj <arne@vajhoej.dk> - 2012-03-27 17:21 -0400
        Re: Keeping the split token in a Java regular expression Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2012-03-27 15:20 -0700
          Re: Keeping the split token in a Java regular expression Arne Vajhøj <arne@vajhoej.dk> - 2012-03-27 18:48 -0400
            Re: Keeping the split token in a Java regular expression Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2012-03-27 17:07 -0700
          Re: Keeping the split token in a Java regular expression Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2012-03-27 21:49 -0300
            Re: Keeping the split token in a Java regular expression Arne Vajhøj <arne@vajhoej.dk> - 2012-03-27 20:56 -0400
              Re: Keeping the split token in a Java regular expression Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2012-03-27 22:01 -0300
                Re: Keeping the split token in a Java regular expression Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2012-03-27 18:27 -0700
  Re: Keeping the split token in a Java regular expression Jim Janney <jjanney@shell.xmission.com> - 2012-03-27 08:15 -0600
    Re: Keeping the split token in a Java regular expression laredotornado <laredotornado@zipmail.com> - 2012-03-27 07:58 -0700
      Re: Keeping the split token in a Java regular expression Jim Janney <jjanney@shell.xmission.com> - 2012-03-27 09:21 -0600
        Re: Keeping the split token in a Java regular expression Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2012-03-27 09:43 -0700
          Re: Keeping the split token in a Java regular expression Robert Klemme <shortcutter@googlemail.com> - 2012-03-28 07:51 +0200

csiph-web