Regexp question: Trouble matching with backslash

Discussion in 'Java' started by Prabh, May 12, 2004.

  1. Prabh

    Prabh Guest

    Hello all,
    I traverse through a file, line-by-line and try to change the path in
    a string which holds path, e.g., if the string is, "File is at
    C:\AppFolder\Folder1\Foo.txt", I'm trying to change Folder1 to
    Folder2, i.e.,

    From: "C:\AppFolder\Folder1\Foo.txt"
    To: "C:\AppFolder\Folder2\Foo.txt"

    Trouble is, I cant even check to see if the string has a match to
    "C:\AppFolder\Folder1....", I suspect because of the backslashes.

    The relevant piece of code:
    =====================================================================
    String getLine = "File is at: C:\AppFolder\Folder1\Foo.txt" ;
    PatternCompiler compiler = new Perl5Compiler() ;
    Pattern pattern = compiler.compile(getLine) ;
    PatternMatcher matcher = new Perl5Matcher() ;

    if ( matcher.matches(getLine, pattern ) )
    {
    System.out.println("Its a match!") ;
    } else {
    System.out.println("It couldnt match!") ;
    }

    =====================================================================

    As you can see, I am basically matching a string against itself, just
    to get going in the java regex learning process. I cant even match a
    string to itself much less do a substitution on it.

    Could some one give me some pointers on how to match and substitute
    Folder1 with Folder2 ?

    Thanks for your time,
    Prabh
    Prabh, May 12, 2004
    #1
    1. Advertising

  2. Prabh

    GaryM Guest

    (Prabh) wrote in news:e7774537.0405121404.739cef03
    @posting.google.com:

    >
    > Could some one give me some pointers on how to match and substitute
    > Folder1 with Folder2 ?
    >


    The problem is how both java and regex interpret the "\" character.

    Try creating a pattern like this and matching it:

    "C:\\\AppFolder\\\Folder2\\\Foo.txt"

    Or just change them to "/"'s :).
    GaryM, May 12, 2004
    #2
    1. Advertising

  3. Prabh

    Chris Smith Guest

    Prabh wrote:
    > The relevant piece of code:
    > =====================================================================
    > String getLine = "File is at: C:\AppFolder\Folder1\Foo.txt" ;
    > PatternCompiler compiler = new Perl5Compiler() ;
    > Pattern pattern = compiler.compile(getLine) ;
    > PatternMatcher matcher = new Perl5Matcher() ;
    >
    > if ( matcher.matches(getLine, pattern ) )
    > {
    > System.out.println("Its a match!") ;
    > } else {
    > System.out.println("It couldnt match!") ;
    > }
    >
    > =====================================================================


    1) That code won't compile. Backslashes have to be escaped in Java
    string literal syntax, making them appear as "\\".

    2) With regular expressions, the backslash is a special character there
    as well, so it would need to be escaped again. That causes it to appear
    as "\\\\" (which, yes, is a little confusing at first, but is a logical
    extension of the Java and Java-regexp languages).

    As an extrapolation on #2, you'd be well-advised to avoid using *any*
    regular expression for matching that wasn't written explicitly as a
    regular expression. Using an arbitrary String as a regexp is gambling
    that the string won't contain characters with special meaning, and
    that's not a safe bet. Regular expressions, then, are useless unless:
    1) you know the pattern at compile-time, or 2) your user knows what a
    regular expression is, knows the Java-specific dialect of Perl regexp
    language, and expects to be entering a regular expression at that point
    in the application. (And this advice even applies to places you might
    expect, such as the replacement string in String.replaceAll and the
    like; though not strictly a regular expression, it does interpret some
    special characters, and hence carries the same set of restrictions.)

    --
    www.designacourse.com
    The Easiest Way to Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
    Chris Smith, May 13, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ladvánszky Károly

    Problem with RE matching backslash

    Ladvánszky Károly, Jan 27, 2004, in forum: Python
    Replies:
    5
    Views:
    589
    Inyeol Lee
    Jan 27, 2004
  2. Vincent Texier
    Replies:
    3
    Views:
    2,262
    Vincent Texier
    Jul 1, 2004
  3. placid

    path backslash escaping trouble

    placid, Jul 10, 2007, in forum: Python
    Replies:
    3
    Views:
    499
    placid
    Jul 12, 2007
  4. Joao Silva
    Replies:
    16
    Views:
    355
    7stud --
    Aug 21, 2009
  5. Marnen Laibow-Koser

    Why does Regexp::escape backslash spaces?!?

    Marnen Laibow-Koser, Jan 26, 2010, in forum: Ruby
    Replies:
    3
    Views:
    116
    Marnen Laibow-Koser
    Jan 26, 2010
Loading...

Share This Page