4

I want split a string like this:

  C:\Program\files\images\flower.jpg     

but, using the following code:

  String[] tokens = s.split("\\");
  String image= tokens[4];

I obtain this error:

 11-07 12:47:35.960: E/AndroidRuntime(6921): java.util.regex.PatternSyntaxException: Syntax error U_REGEX_BAD_ESCAPE_SEQUENCE near index 1:
GVillani82
  • 17,196
  • 30
  • 105
  • 172

7 Answers7

5

try

String s="C:\\Program\\files\\images\\flower.jpg"

String[] tokens = s.split("\\\\");

In java(regex world) \ is a meta character. you should append with an extra \ or enclose it with \Q\E if you want to treat a meta character as a normal character.

below are some of the metacharacters

<([{\^-=$!|]})?*+.>

to treat any of the above listed characters as normal characters you either have to escape them with '\' or enclose them around \Q\E

like:

        \\\\ or \\Q\\\\E
PermGenError
  • 45,977
  • 8
  • 87
  • 106
  • @Joseph82, the reason it works is because a Regex pattern starts and ends with the \ sign, and you need to escape the special char \ so you start and end the pattern with the \ sign, and then the pattern itself is the special char (with escaping) \\. all in all it's \\\\ – thepoosh Nov 07 '12 at 12:05
2

You need to split with \\\\, because the original string should have \\. Try it yourself with the following test case:

    @Test
public void split(){
      String s = "C:\\Program\\files\\images\\flower.jpg";     


        String[] tokens = s.split("\\\\");
        String image= tokens[4];
        assertEquals("flower.jpg",image);
}
mmalmeida
  • 1,037
  • 9
  • 27
1

There is 2 levels of interpreting the string, first the language parser makes it "\", and that's what the regex engine sees and it's invalid because it's an escape sequence without the character to escape.

So you need to use s.split("\\\\"), so that the regex engine sees \\, which in turn means a literal \.

If you are defining that string in a string literal, you must escape the backslashes there as well:

String s = "C:\\Program\\files\\images\\flower.jpg";     
Esailija
  • 138,174
  • 23
  • 272
  • 326
1

String[] tokens=s.split("\\\\");

Venkatesh S
  • 5,466
  • 1
  • 25
  • 30
1

Try this:

String s = "C:/Program/files/images/flower.jpg";
String[] tokens = s.split("/");
enter code hereString image= tokens[4];
Shahinoor Shahin
  • 685
  • 3
  • 14
0

Your original input text should be

 C:\\Program\\files\\images\\flower.jpg  

instead of

 C:\Program\files\images\flower.jpg  
Ahmad
  • 12,336
  • 6
  • 48
  • 88
  • I get the String from a web service. So I insert the path in the string in this way (using Ksoap2): s = response.getProperty("image").toString(); – GVillani82 Nov 07 '12 at 12:09
0

This works,

    public static void main(String[] args) {
        String str = "C:\\Program\\files\\images\\flower.jpg";
        str = str.replace("\\".toCharArray()[0], "/".toCharArray()[0]);
        System.out.println(str);
        String[] tokens  = str.split("/");
        System.out.println(tokens[4]);      
    }
Arham
  • 2,072
  • 2
  • 18
  • 30