469,923 Members | 1,337 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,923 developers. It's quick & easy.

Search string parsing

Hello,

I am having a hard time spliting a string into an array for use in a
search. Here is the situation. The user will input a search string.
Normally I can just split the string by "split /\s+/, $string".
However, I want to allow the user to put words so they appear and are
searched together ie. "search this" would be searched as one term and
not "search" and then somewhere else "this", etc. So if a user enters
something like this:

how do I "search this" and split

How do I split this into an array of:

how
do
I
search this
and
split

Thanks

Fritz
Jul 19 '05 #1
1 5044
flam wrote:
I am having a hard time spliting a string into an array for use in
a search. Here is the situation. The user will input a search
string. Normally I can just split the string by
"split /\s+/, $string". However, I want to allow the user to put
words so they appear and are searched together ie. "search this"
would be searched as one term and not "search" and then somewhere
else "this", etc. So if a user enters something like this:

how do I "search this" and split

How do I split this into an array of:

how
do
I
search this
and
split


This is one way:

my @array;
push @array, $+ while $string =~ /"([^"]*)"|(\S+)/g;

It should be noted that you asked a Frequently Asked Question, so a
more complete answer is available in the Perl FAQ: "How can I split a
[character] delimited string except when inside [character]?"

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Jul 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Lukas Holcik | last post: by
15 posts views Thread by Freddie | last post: by
4 posts views Thread by Axel | last post: by
7 posts views Thread by Bonj | last post: by
1 post views Thread by Raterus | last post: by
6 posts views Thread by Martin Evans | last post: by
1 post views Thread by Eric | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.