• 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

regex expression : how to build a regex query that asks if the String contains a specific word

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

Given the following code :



What regex query should I write as a parameter if I want to look for and find all DVDs with the String "Harry Potter"?

Sincerely,

borkev
 
Ranch Hand
Posts: 180
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are hundreds of tutorials out there for RegEx. But, here is one that directly answers your question:
http://docs.oracle.com/javase/tutorial/essential/regex/literals.html
 
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Maurice Branthome wrote:What regex query should I write as a parameter if I want to look for and find all DVDs with the String "Harry Potter"?


Just use a query of "Harry Potter". Because you are calling find(), it will return true if the pattern occurs anywhere in the given String.

If you had used matches() instead, it would only return true if the pattern matches the given string from beginning to end. In that case you would have had to use a query of ".*Harry Potter.*".
 
Bor Kev
Greenhorn
Posts: 19
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Carey Brown Salil Wadnerkar,

Thanks for taking the time. I had already looked and read many tutorials and had come out by myself with "Harry Potter" as the regex query. The thing is I was doing Unit testing on the find method using the code below :



As the test was failing, I assumed that the regex query "Harry Potter" I was using was wrong. After a little thinking, I realized dvdDatabase.findDVD("Harry Potter") returns a Collection and not a DVD and that is why my unit test was failing and not because the query "Harry Potter" was wrong. Actually the "Harry Potter" regex query was good because when I tested assertEquals(1,(dvdDatabase.findDVD("Harry Potter")).size()) the test went fine.
Again sorry for the confusion.

Sincerely,

borkev


 
Salil Wadnerkar
Ranch Hand
Posts: 180
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The test fails because findDVD() returns a collection of DVDs, but you are testing it for equality with one DVD. Even if your expected collection contains only one element, you still need to test it with a collection.
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you use the literal "Harry Potter", you do not need a regex at all; you can use a String method, maybe contains().

I suggest you use a Stream<DVD> and its filter method and its collect method with this Collector. That will return a List<DVD>; as it says on the adverts, other collectors are also available.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic