I'm using sqlite3 to try and find users who have an e-mail address that is either with Gmail, Yahoo or Hotmail. It needs to do this just on the basis of the first part of the domain, so I want any address that had the #yahoo to be accepted.
It appears from documentation that it is not possible to use a regular expression when querying an sqlite database. Is there any elegant way of doing something similar? It doesn't seem to be possible to use a "like/in" with multiple options (eg: LIKE (%#yahoo%, %#gmail%, %#hotmail%)?
Failing that, I may switch over to MySQL for a reg exp as I want to keep the solution simple and elegant and DB isn't a major factor. How would said regexp query be written in MySQL?
You can't use multiple "LIKE" in that way but you can use:
(email LIKE "%#yahoo%" OR email LIKE "%#gmail%" OR ....)
you can use the way Nemoden is using or something like this (not tested)
WHERE email REGEXP '[\w\.]+#(yahoo|gmail|ymail|hotmail)\.(com|ru|co\.uk)'
This would be much faster because LIKE %string% OR ... is very slow and doesnt use any indexes(dont know if REGEXP uses indexes tho).
I think you might want something like this (RLIKE function):
WHERE email RLIKE '^[A-Z0-9._%+-]+#[A-Z0-9.-]+\.([A-Z]{2}|com|org|net|edu|gov|mil|biz|info|mobi|name|aero|asia|jobs|museum|travel)$'
If you use whatever_cs (case sensitive collation), use a-zA-Z instead of A-Z.
You can also get rid of ^ and $ in the regex. ^ means "starts with" and $ means "ends with"
UPDATE:
'.*#(gmail|hotmail|yahoo)\.[A-Z]{2,4}'
Related
I am using latest version of Server version: 8.0.11 MySQL Community Server - GPL. Download link
Table structure is web_url < id, url >
Sample Data in the url column.
www.google.com
www.yahoo.com
how to extract domain name only like google and yahoo using the function
REGEXP_REPLACE. so it's meaning is the replace the www. and .com part be replace by space or with ''. if you have any other solution then you are welcome.
I am either missing some. So help.
I have written the regular expression but don't getting how use it properly in query. Please give query base answer.
my query
select REGEXP_REPLACE(url,'^(www.)[a-zA-z0-9]*(.[a-zA-Z]{3})(\/[a-zA-Z09-]*)*','') from web_url;
So far i done this query.
So far i done this may be someone get help
done this query to test for comparison is happening or not.
kindly ignore last two rows as they are just for testing purpose of regexp_like function
select regexp_like(url,'^(www.)(.*)(.com)$') from web_url;
If url is misleading and is in fact an FQDN, what the comments suggest,
reverse(regexp_replace(reverse(regexp_replace(url, '\\.[^\\.]*\\.?$', '')), '\\..*', ''))
should give you the second label from the right (or the third if there is the optional empty label at the end), what seems to be what you want.
The reverse() are necessary since we don't know how many dots are in the string. Because of the reverse() after we've removed the the last (or the last two) labels, we can simply remove from the first dot on.
If there were capture groups with regexp_replace() it would be easier, but I didn't find anything about that in the documentation. Please comment and point me to that, if I just missed that.
I am trying to search a store by its name, and when using mysql data binding, it adds a slash in between and prevents me from searching for the store name
The store name is Jimmy's Pita
When I run the query it looks like this
SELECT * FROM stores WHERE store_name = 'Jimmy\'s Pita'
But the store name in the database looks like this ... 'Jimmy's Pita & Poke'
What can I do to fix this issue?
Any help would be really appreciated!
This is to do with escaping. When you specify using single quotes you must escape other single quotes, as in:
'Jimmy\'s Pita'
If you use different quotes, which MySQL allows by default, then you can do this instead:
"Jimmy's Pita"
Both of these are equivalent and in both cases the data saved is:
Jimmy's Pita
The backslash is only there to deal with escaping issues. It is not literally part of the data.
When it comes to searching you may want to use the MySQL full-text index so you can get "close enough" matches, like searching for "jimmys pita" and so on.
I am using MySQL,I have table called constructions where there is a column called phone, phone numbers are in format 9876543210 but I want to clean the column wherever there are alphabets and symbols.
I was using:
select * from constructions where phone like '[a-Z]%';
but this is wrong, i guess it works in SQL-Server.
No to SQL Server unless you add a regular expression utility function. You have the built in find replace functions but those are very simple.
For MySQL look to the reference manual here >>> http://dev.mysql.com/doc/refman/5.0/en/regexp.html
Instead of LIKE use the RLIKE version that works with regular expressions.
In field post_content I have a string like this in nearly 800 rows:
http://somesite.com/">This is some site</a>
I need to remove everything from "> onwards so that it leaves just the URL. I can't do a straight find and replace because the text is unique.
Any clues? This is really my first foray into MySQL database modifications but I did do an extensive search before posting here.
Thanks,
~Kyle~
From this site: http://www.regular-expressions.info/mysql.html
LIB_MYSQLUDF_PREG
If you want more regular expression power in your database, you can consider using LIB_MYSQLUDF_PREG. This is an open source library of MySQL user functions that imports the PCRE library. LIB_MYSQLUDF_PREG is delivered in source code form only. To use it, you'll need to be able to compile it and install it into your MySQL server. Installing this library does not change MySQL's built-in regex support in any way. It merely makes the following additional functions available:
Here it comes...
PREG_CAPTURE extracts a regex match from a string. PREG_POSITION returns the position at which a regular expression matches a string. PREG_REPLACE performs a search-and-replace on a string. PREG_RLIKE tests whether a regex matches a string.
Sounds exactly what you're looking for.
All these functions take a regular expression as their first parameter. This regular expression must be formatted like a Perl regular expression operator. E.g. to test if regex matches the subject case insensitively, you'd use the MySQL code PREG_RLIKE('/regex/i', subject). This is similar to PHP's preg functions, which also require the extra // delimiters for regular expressions inside the PHP string.
See this post: How to do a regular expression replace in MySQL?
Either that or you could just write a script in any lanugage which goes through each record, does a regex replacement and then updates the field. For more info on regex, see here: http://www.regular-expressions.info/reference.html
There's a number of options. One might be to use SUBSTRING_INDEX():
UPDATE
table
SET field = SUBSTRING_INDEX( field, '">', 1 )
It's possible - there is a syntax for User Defined Functions which would let you pass in a regular expression pattern that matches the link and strips everything else.
However, this is quite complicated for somebody new to MySQL, and from your question, this sounds like a one-off. In which case - why not just use Excel and then reimport the data?
Great stuff!
All seems doable with a little bit of time and self education.
In the end, I exported that table as a CSV in Sequel Pro and did some nifty find and replace work in Coda. Not as sophisticated as your suggestions, but it worked.
Thanks again,
~Kyle~
I'm trying to replace all instances of an old BB tag markup in a MySql database with a newer, slightly different one.
The old format is this...
[youtube:********]{Video ID}[/youtube:********]
Which I would like to replace with this...
[youtube:********]http://www.youtube.com/watch?v={Video ID}[/youtube:********]
Where the *'s are a random string of alpha-numeric characters. So simply REPLACE(feild, '[youtube:********]', '[youtube:********]http://www.youtube.com?watch?v= won't do unfortunately.
All the clumsy attempts I've made using REPLACE() and INSTR() have resulted in nasty things like [b]Bold Text[/b]http://www.youtube.com/watch?v=
Is there a way to do this kind of pattern replacement in MySql? Possibly with Regular Expressions?
Thank you.
Is this what you tried?
UPDATE table SET Field = REPLACE(Field,']{',']http://www.youtube.com/watch?v={')
This would depend if there isnt any other occurences of ']{'
EDIT: You may also want to try:
UPDATE table SET Field = LEFT(Field,#) + 'http://www.youtube.com/watch?v='+
RIGHT(Field,(Char_Length(Field)-#);
Just check the syntax with MYSQl docs. Char_LNEGTH() may need to be LENGTH() - im sure you get the idea