• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Right to Left search in Java Regex

 
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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



 
Ranch Hand
Posts: 87
Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
may be you can reverse your input & regex and then match.

Why do you want to do a reverse lookup ?
 
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just as Prem Pillai said, reverse the string and then match.

 
Lucky J Verma
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
eat bricks! HA! And here's another one! And a tiny ad!
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic