I have a rather ugly approach that will strip alphanumeric characters from a user variable
STRIPPING ALPHAS
SET @st='r1+o2l-3a4*n5d6o7';
SELECT (@st:=REPLACE(@st,ch,'')) FROM
(
SELECT (x*10+y) ndx,SUBSTR(chars,x*10+y,1) ch FROM
(SELECT 'abcdefghijklmnopqrstuvwxyz' chars) L,
(SELECT 0 x UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4
UNION SELECT 5 x UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) X,
(SELECT 0 y UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4
UNION SELECT 5 x UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) Y
WHERE SUBSTR(chars,x*10+Y,1)<>'' ORDER BY SUBSTR(chars,x*10+y,1)
) alpha;
SELECT @st;
STRIPPING ALPHAS EXECUTED
mysql> SELECT (@st:=REPLACE(@st,ch,'')) FROM
-> (
-> SELECT (x*10+y) ndx,SUBSTR(chars,x*10+y,1) ch FROM
-> (SELECT 'abcdefghijklmnopqrstuvwxyz' chars) L,
-> (SELECT 0 x UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4
-> UNION SELECT 5 x UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) X
-> (SELECT 0 y UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4
-> UNION SELECT 5 x UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) Y
-> WHERE SUBSTR(chars,x*10+Y,1)<>'' ORDER BY SUBSTR(chars,x*10+y,1)
-> ) alpha;
+---------------------------+
| (@st:=REPLACE(@st,ch,'')) |
+---------------------------+
| r1+o2l-34*n5d6o7 |
| r1+o2l-34*n5d6o7 |
| r1+o2l-34*n5d6o7 |
| r1+o2l-34*n56o7 |
| r1+o2l-34*n56o7 |
| r1+o2l-34*n56o7 |
| r1+o2l-34*n56o7 |
| r1+o2l-34*n56o7 |
| r1+o2l-34*n56o7 |
| r1+o2l-34*n56o7 |
| r1+o2l-34*n56o7 |
| r1+o2-34*n56o7 |
| r1+o2-34*n56o7 |
| r1+o2-34*56o7 |
| r1+2-34*567 |
| r1+2-34*567 |
| r1+2-34*567 |
| 1+2-34*567 |
| 1+2-34*567 |
| 1+2-34*567 |
| 1+2-34*567 |
| 1+2-34*567 |
| 1+2-34*567 |
| 1+2-34*567 |
| 1+2-34*567 |
| 1+2-34*567 |
+---------------------------+
26 rows in set (0.00 sec)
STRIPPING ALPHAS RESULT
mysql> SELECT @st;
+------------+
| @st |
+------------+
| 1+2-34*567 |
+------------+
1 row in set (0.00 sec)
mysql>
STRIPPING NUMERICS
SET @st='r1+o2l-3a4*n5d6o7';
SELECT (@st:=REPLACE(@st,ch,'')) FROM
(
SELECT x+1 ndx,SUBSTR(chars,x+1,1) ch FROM
(SELECT '0123456789' chars) L,
(SELECT 0 x UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4
UNION SELECT 5 x UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) X
) numerics;
SELECT @st;
STRIPPING NUMERICS EXECUTED
mysql> SET @st='r1+o2l-3a4*n5d6o7';
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT (@st:=REPLACE(@st,ch,'')) FROM
-> (
-> SELECT x+1 ndx,SUBSTR(chars,x+1,1) ch FROM
-> (SELECT '0123456789' chars) L,
-> (SELECT 0 x UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4
-> UNION SELECT 5 x UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) X
-> ) numerics;
+---------------------------+
| (@st:=REPLACE(@st,ch,'')) |
+---------------------------+
| r1+o2l-3a4*n5d6o7 |
| r+o2l-3a4*n5d6o7 |
| r+ol-3a4*n5d6o7 |
| r+ol-a4*n5d6o7 |
| r+ol-a*n5d6o7 |
| r+ol-a*nd6o7 |
| r+ol-a*ndo7 |
| r+ol-a*ndo |
| r+ol-a*ndo |
| r+ol-a*ndo |
+---------------------------+
10 rows in set (0.00 sec)
STRIPPING NUMERICS RESULT
mysql> SELECT @st;
+------------+
| @st |
+------------+
| r+ol-a*ndo |
+------------+
1 row in set (0.00 sec)
mysql>
STRIPPING ALPHANUMERICS
SET @st='r1+o2l-3a4*n5d6o7';
SELECT (@st:=REPLACE(@st,ch,'')) FROM
(
SELECT (x*10+y) ndx,SUBSTR(chars,x*10+y,1) ch FROM
(SELECT 'abcdefghijklmnopqrstuvwxyz0123456789' chars) L,
(SELECT 0 x UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4
UNION SELECT 5 x UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) X,
(SELECT 0 y UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4
UNION SELECT 5 x UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) Y
WHERE SUBSTR(chars,x*10+Y,1)<>'' ORDER BY SUBSTR(chars,x*10+y,1)
) alphanumeric;
SELECT @st;
STRIPPING ALPHANUMERICS EXECUTED
mysql> SET @st='r1+o2l-3a4*n5d6o7';
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT (@st:=REPLACE(@st,ch,'')) FROM
-> (
-> SELECT (x*10+y) ndx,SUBSTR(chars,x*10+y,1) ch FROM
-> (SELECT 'abcdefghijklmnopqrstuvwxyz0123456789' chars) L,
-> (SELECT 0 x UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4
-> UNION SELECT 5 x UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) X,
-> (SELECT 0 y UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4
-> UNION SELECT 5 x UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) Y
-> WHERE SUBSTR(chars,x*10+Y,1)<>'' ORDER BY SUBSTR(chars,x*10+y,1)
-> ) alphanumeric;
+---------------------------+
| (@st:=REPLACE(@st,ch,'')) |
+---------------------------+
| r1+o2l-3a4*n5d6o7 |
| r+o2l-3a4*n5d6o7 |
| r+ol-3a4*n5d6o7 |
| r+ol-a4*n5d6o7 |
| r+ol-a*n5d6o7 |
| r+ol-a*nd6o7 |
| r+ol-a*ndo7 |
| r+ol-a*ndo |
| r+ol-a*ndo |
| r+ol-a*ndo |
| r+ol-*ndo |
| r+ol-*ndo |
| r+ol-*ndo |
| r+ol-*no |
| r+ol-*no |
| r+ol-*no |
| r+ol-*no |
| r+ol-*no |
| r+ol-*no |
| r+ol-*no |
| r+ol-*no |
| r+o-*no |
| r+o-*no |
| r+o-*o |
| r+-* |
| r+-* |
| r+-* |
| +-* |
| +-* |
| +-* |
| +-* |
| +-* |
| +-* |
| +-* |
| +-* |
| +-* |
+---------------------------+
36 rows in set (0.00 sec)
STRIPPING ALPHANUMERICS RESULT
mysql> SELECT @st;
+------+
| @st |
+------+
| +-* |
+------+
1 row in set (0.00 sec)
mysql>
EPILOGUE
This SELECT query is meant to hold up to 100 characters for stripping
You can replace (SELECT 'abcdefghijklmnopqrstuvwxyz0123456789' chars) L,
with any list of characters you wish to strip from a user variable.
GIVE IT A TRY !!!
This isn't really a database administration question, but...
Firstly, your pattern does not prevent entries like this: XXXINT123abogus
, because it does not say that "INT" or "NINT" should be at the beginning of the string, neither does it say that the lowercase letter, if it's there, should be the last symbol of the string.
You may want to try something like:
+----------------------------- begins with
|+---------------------------- optional "N"
|| +------------------------- then "INT"
|| | +---------------------- then digits
|| | | +-------------------- minimum 2
|| | | | +------------------ maximum 4
|| | | | | +-------------- then a lowercase letter
|| | | | | | +----------- which is optional
|| | | | | | |+---------- and nothing more
|| | | | | | ||
|| | | | | | ||
'^N?INT\d{2,4}[a-z]?$'
Best Answer
Direct solution:
It is simple enough for do not look for shortening or another optimization, I think.