473,379 Members | 1,302 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

who is good at regular expressions?

my regular expression knowledge is admittedly amateurish ... I have spent an
hour trying to get this to work without any great success, but am in the
middle of a big project so I can't waste a day fiddling around to get it to
work (although I might have to if someone here can't help).

I thought I would throw it out to the group to see if anyone could solve the
problem quick and easy.

I need to break a search string into components to search against a
database.
We want to allow the user to specify database columns by using

[column name][colon][search text]

i.e.
"author:Jane Smith institution:university of Cambridge year:1976"

so basically we have to split the string at the beginning of any word that
ends in a colon.
I need then get an array with column_name=>column_value to construct a SQL
query from.

At the moment I'm just getting garbage or parse errors ... Can anyone help?
Accolades for anyone that can. A pint of lager for anyone in Cambridge that
can!
cheers
Brendan.
Sep 7 '05 #1
8 1343
brendan wrote:
my regular expression knowledge is admittedly amateurish ... I have spent
an hour trying to get this to work without any great success, but am in
the middle of a big project so I can't waste a day fiddling around to get
it to work (although I might have to if someone here can't help).

I thought I would throw it out to the group to see if anyone could solve
the problem quick and easy.

I need to break a search string into components to search against a
database.
We want to allow the user to specify database columns by using

[column name][colon][search text]

i.e.
"author:Jane Smith institution:university of Cambridge year:1976"

so basically we have to split the string at the beginning of any word that
ends in a colon.
I need then get an array with column_name=>column_value to construct a SQL
query from.

At the moment I'm just getting garbage or parse errors ... Can anyone
help? Accolades for anyone that can. A pint of lager for anyone in
Cambridge that can!
cheers
Brendan.

Hi,

Who needs regexp? :P

Try something like this:

<?
// [column name][colon][search text]

$test = "author:Jane Smith institution:university of Cambridge year:1976";
$result = array();

$parts = explode(":",$test);
for($i=0;$i<count($parts);$i+=2){
$result[$parts[$i]] = $parts[$i+1];
}

echo "<pre>".var_dump($result)."</pre>";
?>
Regards,
Erwin Moller

PS: Does your reward also include the ticket to Cambridge from Holland? ;-)
Sep 7 '05 #2
brendan wrote:
We want to allow the user to specify database columns by using

[column name][colon][search text]

i.e.
"author:Jane Smith institution:university of Cambridge year:1976"

so basically we have to split the string at the beginning of any word that
ends in a colon.


Like this?

...
list ($col, $string, $year) = split (":", $input);
...

/Marcin
Sep 7 '05 #3
"Erwin Moller"
<si******************************************@spam yourself.com> wrote in
message news:43***********************@news.xs4all.nl...
We want to allow the user to specify database columns by using

[column name][colon][search text]

i.e.
"author:Jane Smith institution:university of Cambridge year:1976"

Who needs regexp? :P

Try something like this:

<?
// [column name][colon][search text]

$test = "author:Jane Smith institution:university of Cambridge year:1976";
$result = array();

$parts = explode(":",$test);
for($i=0;$i<count($parts);$i+=2){
$result[$parts[$i]] = $parts[$i+1];

Regards,
Erwin Moller

PS: Does your reward also include the ticket to Cambridge from Holland?

;-)

Hi Erwin,
Exploding on the colon will result in the column name ending up in the
previous array set
ie
arrayname["author"]= "Jane Smith institution";
arrayname["university of Cambridge year"1976";
....
which is unfortunately not what I am looking for ... I need it to split at
the begining of any word ending in a colon, not at the colon point.
Unfortunately the offer doesn't include the ticket. However, I'm sure I
could organise delivery next time I am up your way ;)
b.

Sep 7 '05 #4
brendan wrote:
I need to break a search string into components to search against a
database.
We want to allow the user to specify database columns by using

[column name][colon][search text]

i.e.
"author:Jane Smith institution:university of Cambridge year:1976"


Not regexp, more quick'n'dirty but perfectly works and accepts strings in
the format above with as many parameters as required.

All the data is available in a single array ($querydata), see the bottom of
the script:
$string = "author:Jane Smith institution:university of Cambridge year:1976";

$qrykeys = array();
$qryvalues = array();
$tmpwords = array();

$parts = explode(" ",$string);

foreach($parts as $key => $value) {
if (strpos($value,":") !== false) {
if (count($tmpwords) >= 1) {
array_push($qryvalues,implode(" ",$tmpwords));
$tmpwords = array();
}
$subparts = explode(":",$value);
array_push($qrykeys,$subparts[0]);
array_push($tmpwords,$subparts[1]);
}
else {
array_push($tmpwords,$value);
}
}

array_push($qryvalues,implode(" ",$tmpwords));
$tmpwords = array();

$querydata = array();
for ($i = 0; $i!=count($qrykeys) ; $i++) {
$querydata[$qrykeys[$i]] = $qryvalues[$i];
}

print_r($querydata);


HTH
--
Suni
Sep 7 '05 #5
In article <df**********@gemini.csx.cam.ac.uk>,
"brendan" <br*************@srl.cam.ac.uk> wrote:
my regular expression knowledge is admittedly amateurish ... I have spent an
hour trying to get this to work without any great success, but am in the
middle of a big project so I can't waste a day fiddling around to get it to
work (although I might have to if someone here can't help).

I thought I would throw it out to the group to see if anyone could solve the
problem quick and easy.

I need to break a search string into components to search against a
database.
We want to allow the user to specify database columns by using

[column name][colon][search text]

i.e.
"author:Jane Smith institution:university of Cambridge year:1976"

so basically we have to split the string at the beginning of any word that
ends in a colon.
I need then get an array with column_name=>column_value to construct a SQL
query from.

At the moment I'm just getting garbage or parse errors ... Can anyone help?
Accolades for anyone that can. A pint of lager for anyone in Cambridge that
can!
cheers
Brendan.

This is one way to do it:

#!/usr/bin/php
<?
$string = "author:Jane Smith institution:university of Cambridge year:1976";
$m = preg_split("/\s*(\w+):/", $string, -1,
PREG_SPLIT_NO_EMPTY|PREG_SPLIT_DELIM_CAPTURE);
for ($i=0;$i<=count($m)-1;$i+=2){
$sql_query[] = $m[$i] . " = '" . $m[$i+1] . "'";
}
print "select * from database.table where " . join(" and ", $sql_query);
?>

The output:

select * from database.table where author = 'Jane Smith' and institution =
'university of Cambridge' and year = '1976'

--
Sandman[.net]
Sep 7 '05 #6

"Sandman" <mr@sandman.net> wrote in message
news:mr**********************@individual.net...> #!/usr/bin/php
<?
$string = "author:Jane Smith institution:university of Cambridge year:1976"; $m = preg_split("/\s*(\w+):/", $string, -1,
PREG_SPLIT_NO_EMPTY|PREG_SPLIT_DELIM_CAPTURE);
for ($i=0;$i<=count($m)-1;$i+=2){
$sql_query[] = $m[$i] . " = '" . $m[$i+1] . "'";
}
print "select * from database.table where " . join(" and ", $sql_query); ?>

The output:

select * from database.table where author = 'Jane Smith' and institution =
'university of Cambridge' and year = '1976' Sandman[.net]


Thank you sandman! That works perfectly. Beer waiting at the Eagle for you!
regards,
brendan.
Sep 7 '05 #7
That is actually a very tricky regexp to write, because the presence of
the colon at the end of a word changes its meaning, from being part of
the previous values to that of a keyword. With the example given, it'd
be hard to keep the regexp engine from appending "institution" to "Jane
Smith."

The trick here is to reverse the string, then reverse the values after
they've been extracted. Since keywords don't contain white-spaces, it's
easy to tell the regexp where it should stop.

$s = "author:Jane Smith institution:university of Cambridge year:1976";
$r = strrev($s);
preg_match_all("/\s*(.*?)\s*:\s*(\S+)/", $r, $m, PREG_SET_ORDER);
foreach($m as $set) {
$key = strrev($set[2]);
$value = strrev($set[1]);
$values[$key] = $value;
}
print_r($values);

Sep 7 '05 #8
In article <df**********@gemini.csx.cam.ac.uk>,
"brendan" <br*************@srl.cam.ac.uk> wrote:
"Sandman" <mr@sandman.net> wrote in message
news:mr**********************@individual.net...> #!/usr/bin/php
<?
$string = "author:Jane Smith institution:university of Cambridge

year:1976";
$m = preg_split("/\s*(\w+):/", $string, -1,
PREG_SPLIT_NO_EMPTY|PREG_SPLIT_DELIM_CAPTURE);
for ($i=0;$i<=count($m)-1;$i+=2){
$sql_query[] = $m[$i] . " = '" . $m[$i+1] . "'";
}
print "select * from database.table where " . join(" and ",

$sql_query);
?>

The output:

select * from database.table where author = 'Jane Smith' and institution =
'university of Cambridge' and year = '1976'

Sandman[.net]


Thank you sandman! That works perfectly. Beer waiting at the Eagle for you!
regards,


An even more effective way:

#!/usr/bin/php
<?
$string = "author:Jane Smith institution:university of Cambridge year:1976";
$match = preg_split("/ (?=\w+:)/", $string, -1, PREG_SPLIT_DELIM_CAPTURE)
foreach ($match as $m){
list ($field, $value) = split(":", $m);
$sql_query[] = "$field = '$value'";
}
print "select * from database.table where " . join(" and ", $sql_query);
?>
And I don't drink alcohol :)
--
Sandman[.net]
Sep 7 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Danny Lu | last post by:
Hi, can anyone tell me where I can get a good perl book? TIA Dan
4
by: GenoJoe | last post by:
If you are not new to VB.NET but are new to regular expressions, you need to get a free copy of "Pragmatic Guide to Regular Expressions for VB.NET Programmers". I wrote this guide because all of the...
2
by: cleo | last post by:
I'm experimenting with Regular Expressions and Windows Forms. Frequently I want a value to be either a valid pattern or empty. For example a Zip code must be 5 digits or may be empty. I know that...
7
by: norton | last post by:
Hello, Does any one know how to extact the following text into 4 different groups(namely Date, Artist, Album and Quality)? - Artist - Album Artist - Album - Artist - Album - Artist -...
4
by: Együd Csaba | last post by:
Hi All, I'd like to "compress" the following two filter expressions into one - assuming that it makes sense regarding query execution performance. .... where (adate LIKE "2004.01.10 __:30" or...
7
by: Billa | last post by:
Hi, I am replaceing a big string using different regular expressions (see some example at the end of the message). The problem is whenever I apply a "replace" it makes a new copy of string and I...
1
by: Terry Olsen | last post by:
I download xml logs from several servers every day and read the data out of them using the XmlTextReader. But about 10% of them each day throw exceptions because they are not well formed. I don't...
1
by: Allan Ebdrup | last post by:
I have a dynamic list of regular expressions, the expressions don't change very often but they can change. And I have a single string that I want to match the regular expressions against and find...
13
by: Wiseman | last post by:
I'm kind of disappointed with the re regular expressions module. In particular, the lack of support for recursion ( (?R) or (?n) ) is a major drawback to me. There are so many great things that can...
9
by: Rene | last post by:
I'm trying to basically remove chunks of html from a page but I must not be doing my regular expression correctly. What i'm trying with no avail. $site = preg_replace("/<!DOCTYPE(.|\s)*<div...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.