You are viewing a plain text version of this content. The canonical link for it is here.
Posted to regexp-user@jakarta.apache.org by br...@columbia.sc on 2003/04/15 23:29:53 UTC

Expression using backslash?

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: regexp-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: regexp-user-help@jakarta.apache.org


Re: Expression using backslash?

Posted by Raphael Michel <ba...@gmx.net>.
Have you tried with :

String blah  = "This\/is\/a\/test";
RE regexp = new RE("\\/");

or with 

String blah  = "This\\/is\\/a\\/test";
RE regexp = new RE("\\\\/");

??




-- 
Raphael Michel <ba...@gmx.net>

Le mar 15/04/2003 à 23:29, brent@columbia.sc a écrit :
> 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: regexp-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: regexp-user-help@jakarta.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: regexp-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: regexp-user-help@jakarta.apache.org


RE: Expression using backslash?

Posted by Dave Bender <da...@benders-of-edina.com>.
I thought that to find a backslash, RE needed four backslashes.  Not sure
what's happening inside, but that's what worked for me to finding the
backslash.

Dave


-----Original Message-----
From: brent@columbia.sc [mailto:brent@columbia.sc]
Sent: Tuesday, April 15, 2003 4:30 PM
To: regexp-user@jakarta.apache.org
Subject: Expression using backslash?


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: regexp-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: regexp-user-help@jakarta.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: regexp-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: regexp-user-help@jakarta.apache.org


Re: Expression using backslash?

Posted by Chris Gerrard <ch...@gerrard.net>.
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: regexp-user-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: regexp-user-help@jakarta.apache.org