MySQL Regex for excluding string - mysql

Im trying to perform a regex match in mysql to match a string when part of the string is not present. i.e
src="/images/(?legacy)
if the string src="/images/ is present but the string legacy is not.
Any ideas ?

MySQL regexen do not support negative lookaheads, so I think your best bet would be to use two or to use LIKE
string LIKE '%src="/images/%'
AND string NOT LIKE '%src="/images/legacy%'
Note that this query would be inefficient (as it would be using regexen).

I know this is not what you're asking, but MySQL regexes do not support negative lookahead and AFAIK mysql regexes dont use the index.
So i think you may be over thinking it, as the regex would be shorter in writing (if it could work, which it cant) but traditional LIKE is faster.
I suggest you simply use:
WHERE (
content LIKE '%src="/images/%'
AND
content NOT LIKE '%src="/images/legacy%'
)

Related

MySql, search with LIKE %str%

I search my table with query contains LIKE clause %str%.
Is here a way to know where string 'str' was finded in sentence?
I would like to print out 'str' as markup (bold).
For this I need information where exact 'str' begins in any row which contain 'str'.
you can get the string position using the POSITION function ( http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_position ) however thats probably not the best way to do it since if they use the markup more than once it will only return the first position. It would be easier just to replace the string with the string wrapped with whatever markup you want.
If you want an all MySQL solution this would probably work:
SELECT REPLACE(exampleTable.field, 'search_string', '<b>search_string</b>')
FROM exampleTable
WHERE exampleTable.field LIKE '%search_string%';
However i would recommend doing any replacement like this on the PHP / ASP side... using string replacement tools from the respective language.
Sure, you want INSTR() .
You could also use it in your where clause, though you'd want to compare performance between that and LIKE
SELECT INSTR(`field`, 'str') FROM `table` WHERE 0 < INSTR(`field`, 'str')
Remember that INSTR() returns a 1-based index, that is, the first character is postion 1, not position 0; saving 0 for "not found".

MySQL: Find and Replace Between Certain Characters

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~

Mysql full text search exact phrase problem

Using full text search in mysql I'd like to have exact phrase:
"romantic dinner" to be found.
But I also would like each of the words could have synonyms like:
"romantic dinners" to be found for example (our language has great problem where every word has 8 endings like)...
I tried:
+"romantic (dinner dinners)" and
"romantic +(dinner dinners)"
but nothing seems to get results... Is it possible to make some logical OR inside exact search?
UPDATE: TO make it one sentance question: Is there a way to put some logical operators in exact match ("") in full text search?
It will only partially your problem, but there is a soundex() function in mysql which transform the given string to a soundex representation. Similars string should have the same soundex representation so it's maybe a start.
Hope this helps.
If '"roman* dinn*" doesn't work for you, it might be time to look into something like Solr: http://lucene.apache.org/solr/ which will allow for more sophisticated searches, and might already have a stemmer for your language.
SELECT field_name FROM table_name WHERE MATCH(field_name) AGAINST('romantic* dinner*' IN BOOLEAN MODE)...
definitely you can use + and - to further refining your results
reference: http://dev.mysql.com/doc/refman/5.0/en/fulltext-boolean.html

Replacing a formatted string in MySql

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

MySQL: Formatting a string

My database contains a string pattern that is used to allow for easy user editing via a JS-script.
The string is basicly formatted like so:
aaa[bbb#ccc]ddd[eee#fff]ggg
the result I am looking for is
aaacccdddfffggg
I'd like to do this when selecting the string from the database. I'm guessing a regex should do the trick. But my knowledge in the subject of regex's is rather limited. However, this is not a requirement, if there exist a more elegant solution to the problem.
Unfortunately, you can only use a MySQL REGEXP in a WHERE clause, to match against values. You can't use them to transform Strings.
You'll either need to do it clientside, or work with the other String Functions. A MID() would do the trick, if the lengths and positions of the substrings are fixed. If not, use POSITION() (or LOCATE()) to find the special characters []#.