Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Java > Java and JDK > JSP Basics
Password Reminder
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
JSP Basics Beginning-level questions on JSP. More advanced coders should post to Pro JSP.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the JSP Basics section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old December 6th, 2007, 06:11 PM
Registered User
Join Date: Nov 2007
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default String Utils to search all words in filenames

Is there a String Utility out there I could use to search a string with 2 or more words against filenames?

For example, I have this string, "Black Cat", and I have the following filenames:
    "Black Cat.jpg"
    "Cat Is Black.jpg"
    "Cat is black.jpg"
    "Color - black.jpg"
    "Black Catcher.jpg"
    "Black cat22.jpg"
    "Black Cat_22.jpg"

I like to the String Utility to return me True for the 1st, 2nd, 4th, 5th, and 9th filenames.

Please help
Reply With Quote
  #2 (permalink)  
Old December 7th, 2008, 10:39 PM
Registered User
Join Date: Dec 2008
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts

Save the following little ruby script and execute it if you wish.

----- BEGIN SCRIPT -----
#!/usr/bin/ruby -w
# Following solution can be run from the command line, e.g.:
#    $ ./this_program.rb "Black Cat"

def all_words_in_filename?(terms, filename)

  result = false

  # Each matching term cannot be followed by an
  # alphanumeric value in the filename.
  terms.each do |term|
    if filename =~ /#{term}(?![a-z0-9])/i
      result = true
      result = false

  return result

# Presumably, the following hard-coded array would be dynamically
# populated from reading a directory from disc.
file_list = [
             'Black Cat.jpg',
             'Cat Is Black.jpg',
             'Cat is black.jpg',
             'Color - black.jpg',
             'Black Catcher.jpg',
             'Black cat22.jpg',
             'Black Cat_22.jpg',

search_words = ARGV[0].to_s
if search_words.empty?
  puts 'missing multi-word argument'

terms = search_words.split(/\s+/)

i = 1
print "\nSearch Terms: #{terms.join(', ')}\n\n"
file_list.each do |file|
  result = all_words_in_filename?(terms, file)
  a = result ? '*' : ''
  number = (a + i.to_s).rjust(2)
  puts "   #{number}. #{result.to_s.ljust(5)} #{file}"
  i += 1
----- END SCRIPT -----
Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
New text search doesn't preselect search string planoie Visual Studio 2005 0 July 23rd, 2007 06:47 AM
how to extract words from a string in VB.NET? bhavna General .NET 1 January 18th, 2007 01:40 AM
highlight search words lucian Dreamweaver (all versions) 6 January 21st, 2006 04:30 AM
highlight search words lucian Classic ASP Basics 10 August 27th, 2005 09:39 AM
search for multiple words keph Beginning PHP 5 April 6th, 2004 01:23 PM

All times are GMT -4. The time now is 07:15 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.