Results 1 to 9 of 9
Like Tree1Likes
  • 1 Post By jim829

Thread: Regular Expression

  1. #1
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Regular Expression

    I want to split an input string at '\t' and replace '\t' with spaces.
    Java Code:
    String line[] = scn.nextLine().split("[\\t]");
    this line of code doesn't work while ("[\\\\t]") works why ?

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,524
    Rep Power
    5

    Default Re: Regular Expression

    I am not certain what you are doing. Both \t and \\t work for me. That latter doesn't.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: Regular Expression

    I want to read line from file e.g my\tname\tsomething\t ... now I want to split this string at '\t' .. so that it will look like (my name something ) ....

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,524
    Rep Power
    5

    Default Re: Regular Expression

    It still works the same. Are there actual \t's in your file or are you using the tab key? \t is just a symbol for tab like \s is for a space.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: Regular Expression

    yes.. there are actual "\t" in file

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,524
    Rep Power
    5

    Default Re: Regular Expression

    Then answer to your first question is that \t is a special character. It is interpreted by Java as a tab character. If you replace it with \\t, String treats it as
    \t but the regex engine still sees it as a tab character. So you need to use \\\\t which from a String's perspective is \\t and the regex engine treats it as an unspecial delimiter of a backslash followed by a t.

    Having said that, your construct [\\\\t] should not work. This is translated to a character class of a \, and a t. This means that \'s and t's are valid matches. I would just use a string "(?:\\\\t)+" as your regex. It is a non-capturing expression which treats multiple occurrences of \t as one.

    Regards,
    Jim
    gimbal2 likes this.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  7. #7
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Regular Expression

    Sounds like you don't actually want to split the string at all; you just want to replace every occurence of tab with space. Fortunately, there's a method for that: String (Java Platform SE 6)
    Get in the habit of using standard Java naming conventions!

  8. #8
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: Regular Expression

    Quote Originally Posted by kjkrum View Post
    Sounds like you don't actually want to split the string at all; you just want to replace every occurence of tab with space. Fortunately, there's a method for that: String (Java Platform SE 6)
    yup .. I was supposed to do that :)

  9. #9
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,895
    Rep Power
    5

    Default Re: Regular Expression

    Quote Originally Posted by jim829 View Post
    I would just use a string "(?:\\\\t)+" as your regex. It is a non-capturing expression which treats multiple occurrences of \t as one.
    I learned something new. Quite useful.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Regular expression
    By garnaout in forum New To Java
    Replies: 4
    Last Post: 05-15-2012, 06:22 PM
  2. regular expression
    By ehsansad in forum New To Java
    Replies: 8
    Last Post: 03-18-2011, 02:23 PM
  3. Help with regular expression
    By mr.ab18 in forum New To Java
    Replies: 2
    Last Post: 08-06-2010, 10:01 PM
  4. regular expression
    By prof.deedee in forum JDBC
    Replies: 3
    Last Post: 02-19-2010, 11:15 AM
  5. regular expression
    By QkrspCmptPop in forum Advanced Java
    Replies: 8
    Last Post: 01-20-2010, 03:55 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •