Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Regexp question: Trouble matching with backslash
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

Report this thread to moderator Post Follow-up to this message
Old Post
Prabh
05-13-04 02:18 AM


Re: Regexp question: Trouble matching with backslash
Prab_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 :).

Report this thread to moderator Post Follow-up to this message
Old Post
GaryM
05-13-04 02:18 AM


Re: Regexp question: Trouble matching with backslash
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

Report this thread to moderator Post Follow-up to this message
Old Post
Chris Smith
05-13-04 02:18 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Java Programmer archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 11:28 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.