This discussion is archived
1 2 Previous Next 26 Replies Latest reply: Jan 15, 2008 9:05 AM by 3004 RSS

Reading a String Literally - Finding the "\" Character with a Regex

807601 Newbie
Currently Being Moderated
How do I search for "\" characters in a string? Such as..

String text = "Temp\temp.txt";

The problem is that Java will read "\t" as a tab, so that

System.out.println(text);

Will return

Temp emp.txt

Also, searching for the regex "\\\\" will return a null result, presumably because Java interprets the "\t" as a tab character, not as a literal "\" followed by a literal "t".

How do I get Java to read the string without interpreting it?

Thanks in advance
  • 1. Re: Reading a String Literally - Finding the "\" Character with a Regex
    807601 Newbie
    Currently Being Moderated
    you can try using the
    "\\"
    escape sequence to make a \
    Example:
    String text = "Temp\\temp.txt";
  • 2. Re: Reading a String Literally - Finding the "\" Character with a Regex
    807601 Newbie
    Currently Being Moderated
    Couldn't you just use a loop and string.charAt()? Because charAt(5) will still give you a ' \ '.

    Edit: Sorry I tried this and it gave the save undesired result.
    There goes my attempt at helping lol

    Edited by: Annihilator on Jan 14, 2008 7:48 PM
  • 3. Re: Reading a String Literally - Finding the "\" Character with a Regex
    3004 Newbie
    Currently Being Moderated
    Martin_J wrote:
    How do I search for "\" characters in a string? Such as..

    String text = "Temp\temp.txt";

    The problem is that Java will read "\t" as a tab, so that

    System.out.println(text);

    Will return

    Temp emp.txt
    It's not clear what you're asking. If you want a string literal with the \ in it, then
    String text = "Temp\\temp.txt";
    If you want string supplied by a user or read from a file or whatever, then when the user types (or the file contains)
    Temp\temp.txt
    it will work fine, and printing that string will print
    Temp\temp.txt
    . "\t" is only tab character in String and char literals, not in the contents of String objects.
  • 4. Re: Reading a String Literally - Finding the "\" Character with a Regex
    807601 Newbie
    Currently Being Moderated
    The intention here is to get something that would work without changing the string. Lets say that the string is set by the client, so I have got to work with what I get.

    In any case, if I get a string, which may or may not look like Temp\temp.txt, I'll need to see if it includes a "\" before changing it to a "\\".

    If I recall correctly, if this were Perl, it would be the difference between double quotes "Temp\temp.txt" and single quotes 'Temp\temp.txt'.

    This is headed toward the File class. It seems that the File class can read a "\" literally, because it will create a file in the right folder when I use "\" as a file separator. Furthermore it does the right thing whether I use "\" or a "/", and it does not matter if I am running Java on Windows or Mac. So, how does the File class read "\" literally?
  • 5. Re: Reading a String Literally - Finding the "\" Character with a Regex
    807601 Newbie
    Currently Being Moderated
    jverd,

    To respond to your post,

    String text

    oops...
    String text = "Temp\temp.txt"
    System.out.println(text)

    yields...

    Temp emp.txt

    And

    System.out.println("Temp\temp.txt")

    yields

    Temp emp.txt


    Edited by: Martin_J on Jan 14, 2008 11:38 PM
  • 6. Re: Reading a String Literally - Finding the "\" Character with a Regex
    807601 Newbie
    Currently Being Moderated
    Well sure it does. Just as jverd said:

    If you want a '\' char in a string literat you have to use escape sequence.

    So :
    String text = "Temp\\temp.txt";
    System.out.println(text)
    Will show what you desire. And from the other way round if a let's say file contains a string "Temp\temp.txt" then after reading it in Java the '\' will be escaped. You will see "Temp\temp.txt" if you pritn that but internal representation will be the same as for String literal "Temp\\temp.txt".

    HTH
  • 7. Re: Reading a String Literally - Finding the "\" Character with a Regex
    807601 Newbie
    Currently Being Moderated
    The question is not whether I want a "\" in the string, but whether there is a "\" in the string. The posts above all suggest that I change the string, but what if that is not an option?

    As I mentioned above, using the regex "\\\\" returns a null result, because instead of it seeing a "\", it sees a "\t". So, if I try to split the string using "\\\\", I don't get "Temp" and "temp.txt", I get "Temp\temp.txt". You'll note that the file path separator is "\" in Windows systems, which is what I am trying to get to grips with.

    I'm using System.out.println to expose how Java reads the string. For example

    class PrintBackSlash {
         public static void main (String args[]) {
              System.out.println("Test\temp.txt");
              System.out.println("Test\nemp.txt");
              System.out.println("Test\remp.txt");
              System.out.println("Test\\temp.txt");
         }
    }

    yields...

    Test     emp.txt
    Test
    emp.txt
    emp.txt
    Test\temp.txt

    So, how can I write code that splits the string at the right places if it does not contain a "\\" but a "\"? Paraphrased, I want the regex to look for a "\", but not to interpret it as an escape character.
  • 8. Re: Reading a String Literally - Finding the "\" Character with a Regex
    807601 Newbie
    Currently Being Moderated
    PS, If this is impossible, can someone please say so
  • 9. Re: Reading a String Literally - Finding the "\" Character with a Regex
    DarrylBurke Guru Moderator
    Currently Being Moderated
    I think you're still confused about the difference between a String literal and a String object (or variable reference). Run this.
    public class BSTest {
       public static void main (String [] args) {
          String inp = javax.swing.JOptionPane.showInputDialog ("Enter a string with a backslash.");
          System.out.println (inp);
       }
    }
    db
  • 10. Re: Reading a String Literally - Finding the "\" Character with a Regex
    807601 Newbie
    Currently Being Moderated
    Try also this:
    public static void main(String[] args) {
    
              String test1 = "String with a\\t which is not a tab but a t preceeded with a \\ character";
              String test2 = "String with a \t which is a tab";
    
              System.out.println("This are the Strings as user sees / enters them:");
              System.out.println(test1);
              System.out.println(test2);
              System.out.println("");
              System.out.println("***********************************************************");
              System.out.println("");
              System.out
                        .println("Splitting first string using a \\\\\\\\ regex which will be interpreted by the regex engine as \\\\ which will represent a \'\\\' character:");
              System.out.println("");
              for (String s : test1.split("\\\\")) {
                   System.out.println(s);
              }
              System.out.println("");
              System.out.println("***********************************************************");
              System.out.println("");
              System.out.println("Splitting the second string just the same way:");
              System.out.println("");
              for (String s : test2.split("\\\\")) {
                   System.out.println(s);
              }
    
    
         }
    and read the console output.

    It should be:
    This are the Strings as user sees / enters them:
    String with a\t which is not a tab but a t preceeded with a \ character
    String with a       which is a tab

    ***********************************************************

    Splitting first string using a \\\\ regex which will be interpreted by the regex engine as \\ which will represent a '\' character:

    String with a
    t which is not a tab but a t preceeded with a
    character

    ***********************************************************

    Splitting the second string just the same way:

    String with a       which is a tab
  • 11. Re: Reading a String Literally - Finding the "\" Character with a Regex
    807601 Newbie
    Currently Being Moderated
    the problem is that you are specifying the wrong string. your string does not have a backslash in it. it's that simple. so you cannot find one when you search for it.

    because of how the Java language syntax works, there exist things called escape characters, and to specify a literal string in the language with a backslash in it, you need two backslashes in your code; this is purely a language issue and has nothing to do with the concepts of strings

    you are saying, i am going to write it my own way; we have all been telling you, you are writing the wrong code; and you are saying, i don't want to change my code, but it is not doing what i want; well of course; in order to make a program do what you want, you must first learn to use the language correctly
  • 12. Re: Reading a String Literally - Finding the "\" Character with a Regex
    807601 Newbie
    Currently Being Moderated
    Thanks Darryl,

    How come BSTest returns "Temp\test.txt" (which it should), but System.out.println("Temp\test.txt") returns..

    Temp emp.txt

    ??

    Again, the question is not how I change the string to get the right result, but how do I search the string for a "\" (which is not an escape) character?
  • 13. Re: Reading a String Literally - Finding the "\" Character with a Regex
    807601 Newbie
    Currently Being Moderated
    Read my reply to see what's the difference in what you see or write and how it is represented in String objects...

    And the regex should be
    \\\\
    [pre]                                                                                                                                                                                                                                                                                                                           
  • 14. Re: Reading a String Literally - Finding the "\" Character with a Regex
    3004 Newbie
    Currently Being Moderated
    Martin_J wrote:
    The intention here is to get something that would work without changing the string.
    This makes no sense.
    Lets say that the string is set by the client, so I have got to work with what I get.

    In any case, if I get a string, which may or may not look like Temp\temp.txt, I'll need to see if it includes a "\" before changing it to a "\ \".
    Why do you think you need to change it? If the client sends you
    a\t
    then the string will containg 'a', '\', 't', NOT a tab character.
    If I recall correctly, if this were Perl, it would be the difference between double quotes "Temp\temp.txt" and single quotes 'Temp\temp.txt'.
    Maybe, but irrelevant, as Java is not Perl.
    This is headed toward the File class. It seems that the File class can read a "\" literally, because it will create a file in the right folder when I use "\" as a file separator. Furthermore it does the right thing whether I use "\" or a "/", and it does not matter if I am running Java on Windows or Mac. So, how does the File class read "\" literally?
    Again, this makes no sense, and it seems you're confused between String objects and the string literals in your source code that produce them. A \ is NOT an escape character in a String object, only in a string literal.
1 2 Previous Next