Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Regexp question: Trouble matching with backslash

Reply
Thread Tools

Regexp question: Trouble matching with backslash

 
 
Prabh
Guest
Posts: n/a
 
      05-12-2004
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
 
Reply With Quote
 
 
 
 
GaryM
Guest
Posts: n/a
 
      05-12-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) (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 .
 
Reply With Quote
 
 
 
 
Chris Smith
Guest
Posts: n/a
 
      05-12-2004
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why does Regexp::escape backslash spaces?!? Marnen Laibow-Koser Ruby 3 01-26-2010 09:53 PM
[regexp] How to convert string "/regexp/i" to /regexp/i - ? Joao Silva Ruby 16 08-21-2009 05:52 PM
path backslash escaping trouble placid Python 3 07-12-2007 05:31 AM
how to replace double backslash with one backslash in string... Vincent Texier Python 3 07-01-2004 01:03 PM
Problem with RE matching backslash Ladvánszky Károly Python 5 01-27-2004 06:55 PM



Advertisments