| | |
Using REGEXP in searching
Please support our ASP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: Feb 2008
Posts: 174
Reputation:
Solved Threads: 3
Anyone having experience in MySQL and REGEXP ?
I have 1 input field, "what"
Now, the value from the "what" field can contain several substrings and I like each of them to be searched. Can this be done by using REGEXP or do I need to split the "what" input string into arrays before searching ?. Or is there other tricks using the LIKE clause ?
Another issue while talking about REGEXP in MySQL. Let's say I have the input value "e-mail" and the database field contains the value "email". There must be some way to put some variants into the expression to accept the "-" and match the values.
Any examples is welcome.
I have 1 input field, "what"
Now, the value from the "what" field can contain several substrings and I like each of them to be searched. Can this be done by using REGEXP or do I need to split the "what" input string into arrays before searching ?. Or is there other tricks using the LIKE clause ?
Another issue while talking about REGEXP in MySQL. Let's say I have the input value "e-mail" and the database field contains the value "email". There must be some way to put some variants into the expression to accept the "-" and match the values.
Any examples is welcome.
ASP Syntax (Toggle Plain Text)
what = Request.Form("what") WHERE searchwords LIKE ' % " & what & " % ' " WHERE searchwords REGEXP '[[:<:]]"&what&"'"
Last edited by TobbeK; Feb 29th, 2008 at 2:07 pm.
•
•
Join Date: Feb 2008
Posts: 174
Reputation:
Solved Threads: 3
.... just to "simplyfy" my question a bit :-)
If the Input is ("e-mail") then I like to match db column value ("email")
And reverse:
If the Input is ("email") then I like to match db column value ("e-mail")
The string "e-mail" is just an example, it can can be any other words, and the hyphen can be anywhere in the word OR NOT.
I have busted my head off trying some acrobatic expressions for this operations, So any help or examples are extremely welcome.
If the Input is ("e-mail") then I like to match db column value ("email")
And reverse:
If the Input is ("email") then I like to match db column value ("e-mail")
The string "e-mail" is just an example, it can can be any other words, and the hyphen can be anywhere in the word OR NOT.
I have busted my head off trying some acrobatic expressions for this operations, So any help or examples are extremely welcome.
•
•
Join Date: Feb 2008
Posts: 174
Reputation:
Solved Threads: 3
Hi there :-)
Thank you for your time on this.
To handle spaces I also do replacements for that. This part is interesting
becourse it respond to both existing and NON existing spaces in database.
The hyphen "-" in the INPUT does only respond to existing hyphens in the database. Don't know if my thinking or coding is wrong, but the hyphen should be threathed like the space.
As long as the hyphen "-" exists in the INPUT string there is no fuzz.
----------------------------------------
Input:
e-mailmarketing OR e-mail marketing
Output:
e-mailmarketing
e-mail marketing
emailmarketing
email marketing
Now, without the hyphen "-"
----------------------------------------
Input
emailmarketing OR email marketing
Output:
emailmarketing
email marketing
Database query
Thank you for your time on this.
To handle spaces I also do replacements for that. This part is interesting
becourse it respond to both existing and NON existing spaces in database.
The hyphen "-" in the INPUT does only respond to existing hyphens in the database. Don't know if my thinking or coding is wrong, but the hyphen should be threathed like the space.
As long as the hyphen "-" exists in the INPUT string there is no fuzz.
----------------------------------------
Input:
e-mailmarketing OR e-mail marketing
Output:
e-mailmarketing
e-mail marketing
emailmarketing
email marketing
Now, without the hyphen "-"
----------------------------------------
Input
emailmarketing OR email marketing
Output:
emailmarketing
email marketing
ASP Syntax (Toggle Plain Text)
whatinput = "e-mailmarketing" If InStr(whatinput,"-") <> 0 Then whatinput = Replace(whatinput,"-","[\-|\.]?") what = whatinput If InStr(what," ") <> 0 Then what = Replace(what," ","[\ ]?") End If Else what = whatinput End If
Database query
ASP Syntax (Toggle Plain Text)
REGEXP '[[:<:]]"&what&"'"
•
•
Join Date: Sep 2007
Posts: 1,080
Reputation:
Solved Threads: 68
you would have to remove them, or use some very complex regex which would be extremely slow. Something would be like:
[a-Z|-] for each character. You would end up having to search every character, and allow the possibility of it being a hyphen. My suggestion.. forget about it or remove hypens from words, or replace words that are common with a common spelling:
email -> e-mail
It keeps consistency, and no one really cares.
Otherwise you are looking at 10 minute load times for records in excess of 1,000, if those strings are more than 200 characters long.
[a-Z|-] for each character. You would end up having to search every character, and allow the possibility of it being a hyphen. My suggestion.. forget about it or remove hypens from words, or replace words that are common with a common spelling:
email -> e-mail
It keeps consistency, and no one really cares.
Otherwise you are looking at 10 minute load times for records in excess of 1,000, if those strings are more than 200 characters long.
•
•
Join Date: Feb 2008
Posts: 174
Reputation:
Solved Threads: 3
OK, guess you have a good point there.
I will give it a few more shots then I give up. :-)
I know you are extremely talented with database queries.
It would be great if you have a minute to help me analyse this question
I have 2 fields for input
1. what, anything that may identify a company, by name, words etc.
2. where , can contain strings as "statename", or "countyname" or "cityname". So this means, in my case searching for matches in 3 different tables looking for statename, county or city or specific addresses.
Within one single SQL query there is no problem searching for one at time.
I have tried using OR operations, but it doesn't works very well. And I try to limit the input fields to a minimum.
This example doesn't works:
AND KO.county LIKE '%"&where&"%' OR RG.state LIKE '%"&where&"%' "
I don't expect a full specification, but if you have a few hints how to start building on this handle different inputs and searching in different tables and keep the database connections to a minimum.
I will give it a few more shots then I give up. :-)
I know you are extremely talented with database queries.
It would be great if you have a minute to help me analyse this question
I have 2 fields for input
1. what, anything that may identify a company, by name, words etc.
2. where , can contain strings as "statename", or "countyname" or "cityname". So this means, in my case searching for matches in 3 different tables looking for statename, county or city or specific addresses.
Within one single SQL query there is no problem searching for one at time.
I have tried using OR operations, but it doesn't works very well. And I try to limit the input fields to a minimum.
This example doesn't works:
AND KO.county LIKE '%"&where&"%' OR RG.state LIKE '%"&where&"%' "
I don't expect a full specification, but if you have a few hints how to start building on this handle different inputs and searching in different tables and keep the database connections to a minimum.
ASP Syntax (Toggle Plain Text)
SQL = "SELECT CL.organisation, CA.adress,CA.postnr,CA.ort,RG.state,RG.stateID,KO.county,KO.countyID "&_ "FROM tbclients CL, tbclientaddress CA, tbstate RG, tbcounty KO "&_ "WHERE CL.clientID = CA.clientlink "&_ "AND CA.state = RG.stateID "&_ "AND CA.county = KO.countyID "&_ "AND CL.searchwords REGEXP '[[:<:]]"&what&"'" &_ "AND KO.county LIKE '%"&where&"%' "
•
•
Join Date: Sep 2007
Posts: 1,080
Reputation:
Solved Threads: 68
You will only have one connection, but multiple record sets.
Change this a bit to do a more sophisticated join:
Anyway, the reason why this won't work is because you are missing parenthesis:
The reason for that it is failing at the or part, as it has no way to correctly organize the where segment. By adding the parenthesis, you are allowing it to be: (AND county=) or (And state=).
Change this a bit to do a more sophisticated join:
ASP Syntax (Toggle Plain Text)
SQL = "SELECT CL.organisation, CA.adress,CA.postnr,CA.ort,RG.state,RG.stateID,KO.county,KO.countyID "&_ "FROM tbclientaddress CA JOIN tbclients CL ON CA.clientlink = CL.clientID JOIN tbstate RG ON CA.state = RG.stateID JOIN tbcounty KO ON CA.county = KO.countyID "&_ "WHERE CL.searchwords REGEXP '[[:<:]]"&what&"'" &_ "AND KO.county LIKE '%"&where&"%' "
Anyway, the reason why this won't work is because you are missing parenthesis:
AND (KO.county LIKE '%"&where&"%' OR RG.state LIKE '%"&where&"%') "
![]() |
Similar Threads
- Replace a space with an underscore SOLVED (PHP)
- Limiting grep searches to a given file type? (IT Professionals' Lounge)
- Desiging a set of rules for a match (C++)
Other Threads in the ASP Forum
- Previous Thread: creating download object.
- Next Thread: Updating table in a remote database
| Thread Tools | Search this Thread |
archive asp asp.net aspandmssqlserver2005 aspandmssqlserver2005connection aspconnection connection database databaseconnection diagnostics dreamweaver excel fso iis microsoft msmsql mssql2005 mssqlserver2005 mssqlserver2005andasp mssqlserverandasp opentextfile record searchbox selectoption server single specfic sqlserver sqlserverconnection toolkit webserver windows7






