• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Search String

 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I am trying to search string in a ArrayList of strings as we search a database with wildcards.

For example
Suppose Actual String is: - HTML is not enabled
Search query 1: %HTML%
Search query 2 : %TM% is not %nab%
Search query 3 : HTML is *o* (* and % both same (like wild card searches)
Search query 4 : HTML not enabled.

Of the above first 3 should give the result as true except the last.

I had tried the option of matches with regular expression, by adding (?i) at the starting of the string and replacing * or % with .* in the entire string. The problem i am facing now is that i am not able to do a replace of a special character (% or *) with another special character (.*).

I would like to know if there is any other better approach for this and also the solution for replacing a special character with another special character.

Thanks in Advance
 
Ranch Hand
Posts: 1179
Mac OS X Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You repalce 'special character' by adding '\\' infront of the character - like this:
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Remember to first replace * with .*, then % with .*
If you do it the other way around, % will turn into ..* - since you are replacing its * with .* as well.

I've made that mistake sometimes yes. Quote difficult to debug at times too.
 
reply
    Bookmark Topic Watch Topic
  • New Topic