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. Advertisements

  2. Prabh

    GaryM Guest

    (Prabh) wrote in @posting.google.com:
    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. Advertisements

  3. Prabh

    Chris Smith Guest

    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. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.