Welcome to WebmasterWorld Guest from 54.162.117.84

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)

Junior Member

10+ Year Member

joined:Nov 4, 2003
posts:91
votes: 0


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?

Thanks

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

Preferred Member

10+ Year Member

joined:Jan 5, 2006
posts:536
votes: 0


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)

Junior Member

10+ Year Member

joined:Nov 4, 2003
posts:91
votes: 0


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

cheers

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

Preferred Member

10+ Year Member

joined:Jan 5, 2006
posts:536
votes: 0


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;
return($string);
}
12:38 am on Mar 25, 2006 (gmt 0)

Junior Member

10+ Year Member

joined:Nov 4, 2003
posts:91
votes: 0


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

Administrator from US 

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

joined:Sept 21, 1999
posts:38061
votes: 13


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