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

Need help with regexp

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

I would like to create a regular expression that will match the following use case:
Suppose I have got a paragraph and a String as a search term. What I would like is to apply a regexp on the paragraph to let me know if the paragraph contains the search term itself or any of its words.
So, suppose the search term is "hello world" I would like to check if the paragraph contains the String "hello world" or any of the words "hello" or "wold".
The way I do it now is split the term by space and then search for each word separately however I am guessing that this can be done in much more elegant way using regexp and in one go.
Any ideas...?

 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could just do a regex for those phrases, e.g.:



 
Andrew Carney
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's what I'm doing now however I break the term into words using String.split, is there a way for regexp to do this for me and include it in the same expression?
 
Greenhorn
Posts: 10
Mac OS X IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could use indexOf method of String class


PS: note the space while searching(" world"). If you don't give the space, then even search for "World" in string "HelloWorld" would return found.
 
Anooj Narvekar
Greenhorn
Posts: 10
Mac OS X IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Regex should be used when you want to search something based on a pattern. In this case, regex might be an overhead(negligible though).
 
When evil is afoot and you don't have any arms you gotta be hip and do the legwork, but always kick some ... tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic