Re: Expression using backslash?

Chris Gerrard <[email protected]> Tue, 15 Apr 2003 18:48:04 -0400
Newsgroups gmane.comp.jakarta.regexp.user
Message-ID <[email protected]>
This is interesting.

In order to get the behavior you want I coded:

         String   blah = "This\\/is\\/a\\/test";  // reduces to: 
"This\/is\/a\/test"
         blah.split("\\\\/");                     // reduces to: "\\/"

see the test.java class below.

As near as I can figure out, the initial String "\\\\/" gets reduced (is 
there a proper term here?) to "\\/" when it's interpreted by the compiler; 
this String then gets passed into the split() method, (which I can only 
find for String & wonder what your RE class is) where it undergoes another 
reduction to "\/" and is then used in the splitting.

I'm looking at the regex JavaDoc & there's some description of the 
backslash & escaping behavior, but I've not puzzled it all out yet.

Anyway, try test.java to see if it help.

test;java:

public class test {

     public static void main(String[] args) {

         String   blah = "This\\/is\\/a\\/test";
         split(blah, "\\/");
         split(blah, "\\\\/");
         split(blah, "\\\\\\/");

       // These will not compile
       // String blah = "This\\\/is\\/a\\/test";
       // split(blah, "\/");
       // split(blah, "\\\/");
       // split(blah, "\\\\\/");
     }

     private static void split(String string, String regex) {
         System.out.println("\n--");
         System.out.println("This String: " + string);
         System.out.println(" split with: " + regex);

         String[] splitString = string.split(regex);
         System.out.println("   produces: ");
         print(splitString);
     }

     private static void print(String[] strings) {

         for (int i=0; i<strings.length; i++) {
             System.out.println(strings[i]);
         }
     }

}  //..class test

HTH

Chris


At 05:29 PM 4/15/2003, you wrote:
>I'm having some issues attempting to split on a regular expression that
>contains a backslash.  Here is an example (I want to split on "\/" - an 
>actual
>backslash then forwardslash)..
>
>String blah  = "This\\/is\\/a\\/test";
>RE regexp = new RE("\\/");
>String[] temp = regexp.split(blah);
>
>If you look at the resulting string array you'll see this:
>
>This\
>is\
>a\
>test
>
>Now why are the backslashes in there?  I thought when you ran a split it was
>support to remove all regexp chars?
>
>Thanks,
>
>- Brent
>
>-------------------------------------------------
>Get your very own filtered email account!
>http://www.columbia.sc
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: [email protected]
>For additional commands, e-mail: [email protected]