Skip navigation links

MySQL Forums :: Newbie :: Regexp, replace ??? what do I need to use


Advanced Search

Re: Regexp, replace ??? what do I need to use
Posted by: Felix Geerinckx ()
Date: January 30, 2007 01:40PM

Sandra Rascheli Wrote:

> Hello everybody, I am new to regular expressions
> and I need to know if they can help in solving
> this problem, I have a field in a table called
> "identification" which is a text field that
> includes dots (.), commas (,), hyphens (-) and
> letters, plus numeric characters. What I want to
> do is to extract ONLY the numeric characters from
> the field.

Unfortunately, they cannot.
If you are using MySQL >= 5, your best bet is writing a stored procedure.

Alternatively, you can use something silly like this:
USE test;

-- Helper table
DROP TABLE IF EXISTS ints;
CREATE TABLE ints (i INT UNSIGNED NOT NULL PRIMARY KEY);

-- make sure you have enough of them
INSERT INTO ints (i) VALUES
( 1), ( 2), ( 3), ( 4), ( 5), ( 6), ( 7), ( 8), ( 9), (10),
(11), (12), (13), (14), (15), (16), (17), (18), (19), (20);

DROP TABLE foo;
CREATE TABLE foo (identification CHAR(20) NOT NULL);
INSERT INTO foo (identification) VALUES
('V-1.234.567'),
('X-ABC-123.9');

SELECT
    identification, 
    GROUP_CONCAT(SUBSTRING(identification, i, 1) ORDER BY i SEPARATOR '')
FROM foo
JOIN ints ON i BETWEEN 1 AND LENGTH(identification)
WHERE
    SUBSTRING(identification, i, 1) IN ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9')
GROUP BY identification;

In my opinion, MySQL urgently needs some powerful builtin search-and-replace string functions, preferably based on a regex engine.

--
felix
Please use BBCode to format your messages in this forum.

Options: ReplyQuote


Subject Written By Posted
Regexp, replace ??? what do I need to use Sandra Rascheli 01/30/2007 10:37AM
Re: Regexp, replace ??? what do I need to use Felix Geerinckx 01/30/2007 01:40PM
Re: Regexp, replace ??? what do I need to use Sandra Rascheli 01/30/2007 06:15PM
Re: Regexp, replace ??? what do I need to use Sandra Rascheli 02/06/2007 10:51AM
Re: Regexp, replace ??? what do I need to use Felix Geerinckx 02/06/2007 11:56AM
Re: Regexp, replace ??? what do I need to use Sandra Rascheli 02/06/2007 06:05PM
Re: Regexp, replace ??? what do I need to use Felix Geerinckx 02/07/2007 02:06AM
Re: Regexp, replace ??? what do I need to use Sandra Rascheli 02/07/2007 06:02AM
Re: Regexp, replace ??? what do I need to use sumit durugkar 01/17/2008 01:08AM
Re: Regexp, replace ??? what do I need to use Don Rapp 01/19/2008 09:57PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.