Split string by regex
am 03.08.2010 17:03:53 von Adam Gray
Hello,
I'm working on a library OPAC system with books classified using the Library of Congress classification system. This takes the format of either one or two letters followed by some numbers, i.e. R272 or RA440 etc. What I want to do is split the field that holds this classification into two, one containing the letter portion and the other containing the number bit. So
+-------+
| Class |
+-------+
| R100 |
+-------+
| RA65 |
+-------+
| RP2 |
+-------+
Would become
+--------+--------+
| Class | Class2 |
+--------+--------+
| R | 100 |
+--------+--------+
| RA | 65 |
+--------+--------+
| RP | 2 |
+--------+--------+
etc
Could this be done in MySQL? I want to do something along the lines of set class2 = SUBSTRING_INDEX(class,'[A-Z]',-1) but I understand this is not possible.
Any ideas?
Regards
Adam
--
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe: http://lists.mysql.com/mysql?unsub=gcdmg-mysql-2@m.gmane.org
RE: Split string by regex
am 03.08.2010 20:38:00 von Gavin Towey
Since your conditions are pretty specific you can do this:
set @a=3D'RA100';
select IF(@a REGEXP '[A-Za-z]{2}', SUBSTRING(@a,1,2), SUBSTRING(@a,1,1)) as=
letter, IF(@a REGEXP '[A-Za-z]{2}', SUBSTRING(@a,3), SUBSTRING(@a,2));
+--------+--------+
| letter | number |
+--------+--------+
| RA | 100 |
+--------+--------+
If this looks ugly, then that should be good motivation not to store multip=
le pieces of data as concatenated strings =3D)
Regards,
Gavin Towey
-----Original Message-----
From: Adam Gray [mailto:acgray@me.com]
Sent: Tuesday, August 03, 2010 8:04 AM
To: mysql@lists.mysql.com
Subject: Split string by regex
Hello,
I'm working on a library OPAC system with books classified using the Librar=
y of Congress classification system. This takes the format of either one or=
two letters followed by some numbers, i.e. R272 or RA440 etc. What I want =
to do is split the field that holds this classification into two, one conta=
ining the letter portion and the other containing the number bit. So
+-------+
| Class |
+-------+
| R100 |
+-------+
| RA65 |
+-------+
| RP2 |
+-------+
Would become
+--------+--------+
| Class | Class2 |
+--------+--------+
| R | 100 |
+--------+--------+
| RA | 65 |
+--------+--------+
| RP | 2 |
+--------+--------+
etc
Could this be done in MySQL? I want to do something along the lines of set =
class2 =3D SUBSTRING_INDEX(class,'[A-Z]',-1) but I understand this is not p=
ossible.
Any ideas?
Regards
Adam
--
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe: http://lists.mysql.com/mysql?unsub=3Dgtowey@ffn.com
This message contains confidential information and is intended only for the=
individual named. If you are not the named addressee, you are notified th=
at reviewing, disseminating, disclosing, copying or distributing this e-mai=
l is strictly prohibited. Please notify the sender immediately by e-mail i=
f you have received this e-mail by mistake and delete this e-mail from your=
system. E-mail transmission cannot be guaranteed to be secure or error-fre=
e as information could be intercepted, corrupted, lost, destroyed, arrive l=
ate or incomplete, or contain viruses. The sender therefore does not accept=
liability for any loss or damage caused by viruses or errors or omissions =
in the contents of this message, which arise as a result of e-mail transmis=
sion. [FriendFinder Networks, Inc., 220 Humbolt court, Sunnyvale, CA 94089,=
USA, FriendFinder.com
--
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe: http://lists.mysql.com/mysql?unsub=3Dgcdmg-mysql-2@m.gmane.o rg