aspose file tools*
The moose likes Java in General and the fly likes Right to Left search in Java Regex Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Right to Left search in Java Regex" Watch "Right to Left search in Java Regex" New topic
Author

Right to Left search in Java Regex

Lucky J Verma
Ranch Hand

Joined: Apr 11, 2007
Posts: 278
I want to do right to left backwards search as in .net.
My regex is ^(.*\\S+.*$) .
How can i implement it in Java?In .net We have RegexOptions.RighttoLeft and Java regex engine doesnt allow backward searching.
So i am looking for alternatives.

Thanks



prem pillai
Ranch Hand

Joined: Nov 02, 2007
Posts: 87

may be you can reverse your input & regex and then match.

Why do you want to do a reverse lookup ?
Martin Vanyavchich
Ranch Hand

Joined: Sep 16, 2008
Posts: 241
Just as Prem Pillai said, reverse the string and then match.



SCJP 6, OCMJD 6, OCPJWSD 6
I no good English.
Lucky J Verma
Ranch Hand

Joined: Apr 11, 2007
Posts: 278
I did the same ,reversed the string and reversed it back after output.

To Pillai's Qn- I need to do Backward search because i look for a search word in a document and then pick previous 2 lines and next 2 lines and current line.
So for previous lines, i do backward matching.

How much good it is in performance wise to do this.-
reverse the string ,
reverse the pattern (I dint need this ,because i search line by line)
Mapping the index
Search and

then reverse back and also i am using collection to pick up all the lines ,reverse collection too.

Is there any other efficient method?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18717
    
  40


Yeah... the .NET backward searching feature is nifty isn't it?

Another option, is to do a forward search, and save the results somewhere. Then use a loop that will iterate the results backwards.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Right to Left search in Java Regex