Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > JSP Basics
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 6th, 2007, 06:11 PM
Registered User
 
Join Date: Nov 2007
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"
    "Black Cat.jpg"
    "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
 
Old December 7th, 2008, 10:39 PM
Registered User
 
Join Date: Dec 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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

Code:
----- 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
    else
      result = false
      break
    end
  end

  return result
end

# Presumably, the following hard-coded array would be dynamically
# populated from reading a directory from disc.
file_list = [
             'Black_Cat.jpg',
             'Black Cat.jpg',
             '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'
  exit
end

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
----- END SCRIPT -----





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





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