Code Comments
Programming Forum and web based access to our favorite programming groups.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
Post Follow-up to this messagePrab_kar@hotmail.com (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 :).
Post Follow-up to this messagePrabh 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
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.