If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Replace Regexp

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-30-05, 09:38
gtk gtk is offline
Registered User
 
Join Date: Oct 2004
Location: Paris, FRANCE
Posts: 132
Post Replace Regexp

Having this in a table "Titles":
Code:
Title
-------
The Labor
Der Lancet
La xxxx
Sciences
.
.
'd like to UPDATE Title by putting the start "The |Der |La " at the end of the string in one go, like "Labor (The) | Lancet (Der) | ...".

Any way of using "REPLACE or TRIM" combined with "REGEXP" '^(The |DER |La )' ?

something like this:
Code:
UPDATE Titles SET Title=REPLACE (Title, REGEXP......, '')
Reply With Quote
  #2 (permalink)  
Old 09-30-05, 12:28
felixg felixg is offline
Registered User
 
Join Date: Apr 2005
Location: Lier, Belgium
Posts: 122
Quote:
Originally Posted by gtk

Any way of using "REPLACE or TRIM" combined with "REGEXP" '^(The |DER |La )' ?
Unfortunately, MySQL regular expression don't provide that kind of power at the moment.

However, if you put your prefixes in a table:
Code:
CREATE TABLE prefixes (
	prefix CHAR(10) NOT NULL PRIMARY KEY,
	plength TINYINT NOT NULL DEFAULT 0,
	KEY (plength)
);

INSERT INTO prefixes (prefix) VALUES
('De'), ('The'), ('Der'), ('La'), ('Le');

UPDATE prefixes SET plength = LENGTH(prefix);
you could use something like this:
Code:
UPDATE titles AS t
SET t.title = CONCAT(
	SUBSTRING(
		t.title, 
		2 + (
				SELECT p.plength 
				FROM prefixes AS p 
				WHERE LOCATE(p.prefix, t.title) = 1 
				ORDER BY p.plength DESC 
				LIMIT 1
			)
	), 
	' (',
	(
		SELECT p.prefix 
		FROM prefixes AS p 
		WHERE LOCATE(p.prefix, t.title) = 1 
		ORDER BY p.plength DESC 
		LIMIT 1
	),
	')'
	)
WHERE
	EXISTS (
		SELECT * 
		FROM prefixes AS p 
		WHERE LOCATE(CONCAT(p.prefix, ' '), t.title) = 1
	);
--
felix
Reply With Quote
  #3 (permalink)  
Old 09-30-05, 13:06
felixg felixg is offline
Registered User
 
Join Date: Apr 2005
Location: Lier, Belgium
Posts: 122
Quote:
Originally Posted by felixg
you could use something like this:
Code:
Overly complex code snipped
or the much simpler
Code:
UPDATE titles t, prefixes p
SET t.title = CONCAT(SUBSTRING(t.title, p.plength+2), ' (', p.prefix, ')')
WHERE
	SUBSTRING(t.title, 1, p.plength+1) = CONCAT(p.prefix, ' ');

--
felix[/QUOTE]
Reply With Quote
  #4 (permalink)  
Old 10-03-05, 03:16
gtk gtk is offline
Registered User
 
Join Date: Oct 2004
Location: Paris, FRANCE
Posts: 132
Thank you for your time,
in fact it was just to know if "regexp replace" is possible.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On