Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k & phranque

Message Too Old, No Replies

parsing a search string

treating stuff in quote marks as one word



11:09 am on Mar 24, 2006 (gmt 0)

10+ Year Member

Is there a preferred way of doing this?

At the moment, I'm splitting the string into words, then going through each word one by one.

If the word starts with a quote mark, add it to the next word until it finds a word ending in a quote mark.

Is there a better way?



7:18 pm on Mar 24, 2006 (gmt 0)

5+ Year Member

the question is really too vague to answer, post some sample data you are searching and the code you are currently using and the results you are trying to achieve.


8:11 pm on Mar 24, 2006 (gmt 0)

10+ Year Member

sorry, I'll try to be more concise.

If I have a search string, such as:

decorated "blue widgets" "red widgets" sandy leaf.

What is the best way of splitting that string up, so that I have an array with the following data:

- decorated
- blue widgets
- red widgets
- sandy
- leaf

currently, I'm splitting the string with split(/ /) to get the individual words, then piecing together the quoted words



10:05 pm on Mar 24, 2006 (gmt 0)

5+ Year Member

maybe there is a better way, but here is one way:

my $var = qq~decorated "blue widgets" "red widgets" sandy leaf.~;
$var =~ s/("[^\"]*")/unspacify($1)/ge;
my @array = split(/\s+/,$var);
s/\^//g for @array;
print map{"$_\n"} @array;
sub unspacify {
#replaces spaces within quoted words with ^
my $string = shift;
$string =~ s/\s/\^/g;


12:38 am on Mar 25, 2006 (gmt 0)

10+ Year Member

thank you


4:27 am on Mar 27, 2006 (gmt 0)

WebmasterWorld Administrator brett_tabke is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

Then if you want to get fancy - add +- altavista syntax...

Featured Threads

Hot Threads This Week

Hot Threads This Month