• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

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).
 
What? What, what, what? What what tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic