473,770 Members | 4,443 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5207
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2355
by: Lukas Holcik | last post by:
Hi everyone! How can I simply search text for regexps (lets say <a href="(.*?)">(.*?)</a>) and save all URLs(1) and link contents(2) in a dictionary { name : URL}? In a single pass if it could. Or how can I replace the html &entities; in a string "blablabla&amp;blablabal&amp;balbalbal" with the chars they mean using re.sub? I found out they are stored in an dict . I though about this functionality:
15
3631
by: Freddie | last post by:
Happy new year! Since I have run out of alcohol, I'll ask a question that I haven't really worked out an answer for yet. Is there an elegant way to turn something like: > moo cow "farmer john" -zug into: ,
15
3624
by: SK | last post by:
Hey folks, I am searching for a string (say "ABC") backwards in a file. First I seek to the end. Then I try to make a check like - do { file.clear (); file.get(c); file.seekg(-2, std::ios::cur);
4
9679
by: Axel | last post by:
Is it possible to write a Stored Procedure that takes a string of search keywords as argument and returns the recordset? At the mo I am passing the WHERE String as argument. I got this technique from an Extreme Ultradev tutorial by Rick Curtis it looked quite ok: http://www.princeton.edu/~rcurtis/ultradev/tutorial12.html I have to admit, one of the main reason for passing the WHERE string is that I do not know how to do the string...
7
1943
by: Bonj | last post by:
In making a ternary search tree to identify as fast as possible the type of word passed in to the algorithm, for instance sp_help -> 1 (procedures), select -> 2 (keywords), sysobjects -> 3 (system tables), I'm stuck on deciding what type of comparison happens at each node. Has anyone got any suggestions? Do you think for the first level I could compare just the first characters of the word passed in and the word at the 'current' node, or...
1
1055
by: Raterus | last post by:
Does anyone know of some code samples that will parse a string field into an array of strings, but do it like a search engine would. I'm sure this would be fun to do it myself, but if an algorithm already exists that would do a much better job, I'm all for it. Basically it would take something like this "a field", another field, search on this, "all of this" and turn it into a string array like this, ignoring comma's, and treating...
6
5897
by: Martin Evans | last post by:
Sorry, yet another REGEX question. I've been struggling with trying to get a regular expression to do the following example in Python: Search and replace all instances of "sleeping" with "dead". This parrot is sleeping. Really, it is sleeping. to This parrot is dead. Really, it is dead.
1
2723
by: Eric | last post by:
Hi: I have two files. I search pattern ":" from emails text file and save email contents into a database. Another search pattern " field is blank. Please try again.", vbExclamation + vbOKOnly Me.txtEmail.SetFocus Exit Sub End If Me.txtStatusBar.Value = "Parsing..." strEmail = Me.txtEmail.Value
8
5118
by: Sham | last post by:
I am trying to perform the following query on a table that has been indexed using Full Text Search. The table contains multiple columns than have been indexed. (Below, all xml columns are indexed). dbo.maintable(ProfileID int pk) dbo.fts_table(ProfileID int pk fk, col1 xml, col2 xml, col3 xml) I want to perform a query that will return any rows that contain ‘x’ and ‘y’ in any columns. I.e. ‘x’ could be in col1 and ‘y’ could be in
0
9602
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9439
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10237
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10071
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
6690
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5467
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3987
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3589
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2832
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.