Re: mini string search library
Roedy Green wrote:
You might find these simple string searching tools useful,
particularly if you do a lot of screen scraping.
package com.mindprod.poster;
/**
* Methods for searching strings for multiple targets.
* Especially useful for screen scraping.
*/
public class StringSearch
{
// -------------------------- PUBLIC STATIC METHODS
--------------------------
/**
* find first of a number of possible targets.
*
* @param s String to search in
* @param targets multiple targets to search for
* @return index of the first matching target, -1 if none of the
targets match.
*/
public static int indexOf( String s, String... targets )
{
int result = -1;
for ( String target : targets )
{
final int place = s.indexOf( target );
if ( 0 <= place && ( place < result || result < 0 ) )
{
result = place;
}
}
return result;
}
/**
* find first of a number of possible targets.
*
* @param s String to search in
* @param base offset where to start looking
* @param targets multiple targets to search for
* @return index of the first matching target, -1 if none of the
targets match.
*/
public static int indexOf( String s, int base, String... targets )
{
int result = -1;
for ( String target : targets )
{
final int place = s.indexOf( target, base );
if ( 0 <= place && ( place < result || result < 0 ) )
{
result = place;
}
}
return result;
}
/**
* find first of a number of possible targets
*
* @param s String to search in
* @param targets multiple targets to search for
* @return index of char one past the end of the first matching
target, -1 if none of the targets match.
*/
public static int indexOfEnd( String s, String... targets )
{
int result = -1;
int length = 0;
for ( String target : targets )
{
final int place = s.indexOf( target );
if ( 0 <= place && ( place < result || result < 0 ) )
{
result = place;
length = target.length();
}
}
return result + length;
}
/**
* find first of a number of possible targets
*
* @param s String to search in
* @param base offset where to start looking
* @param targets multiple targets to search for
* @return index of char one past the end of the first matching
target, -1 if none of the targets match.
*/
public static int indexOfEnd( String s, int base, String...
targets )
{
int result = -1;
int length = 0;
for ( String target : targets )
{
final int place = s.indexOf( target, base );
if ( 0 <= place && ( place < result || result < 0 ) )
{
result = place;
length = target.length();
}
}
return result + length;
}
/**
* find last of a number of possible targets, one closest to the
end
*
* @param s String to search in
* @param targets multiple targets to search for
* @return index of the first matching target, -1 if none of the
targets match.
*/
public static int lastIndexOf( String s, String... targets )
{
int result = -1;
for ( String target : targets )
{
final int place = s.lastIndexOf( target );
if ( 0 <= place && ( result < place || result < 0 ) )
{
result = place;
}
}
return result;
}
/**
* find last of a number of possible targets, one closest to the
end
*
* @param s String to search in
* @param base offset where to start looking
* @param targets multiple targets to search for
* @return index of the first matching target, -1 if none of the
targets match.
*/
public static int lastIndexOf( String s, int base, String...
targets )
{
int result = -1;
for ( String target : targets )
{
final int place = s.lastIndexOf( target, base );
if ( 0 <= place && ( result < place || result < 0 ) )
{
result = place;
}
}
return result;
}
/**
* find last of a number of possible targets, one closest to the
end
*
* @param s String to search in
* @param targets multiple targets to search for
* @return index of the char one past the matching target that
starts closest to the end, -1 if none of the targets match.
*/
public static int lastIndexOfEnd( String s, String... targets )
{
int result = -1;
int length = 0;
for ( String target : targets )
{
final int place = s.lastIndexOf( target );
if ( 0 <= place && ( result < place || result < 0 ) )
{
result = place;
length = target.length();
}
}
return result + length;
}
/**
* find last of a number of possible targets, one closest to the
end
*
* @param s String to search in
* @param base offset where to start looking
* @param targets multiple targets to search for
* @return index of the char one past the matching target that
starts closest to the end, -1 if none of the targets match.
*/
public static int lastIndexOfEnd( String s, int base, String...
targets )
{
int result = -1;
int length = 0;
for ( String target : targets )
{
final int place = s.lastIndexOf( target, base );
if ( 0 <= place && ( result < place || result < 0 ) )
{
result = place;
length = target.length();
}
}
return result + length;
}
}
Nice, although for such a wide-use library, I think I would avoid an
o(n*m*l) algorithm. I might start out by building an FSA from the
targets, and running the input through it. This approach is O((m*l) +
(n)) (where n is the s.length, l is the average length of the targets,
and m is the number of targets)
--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>