Skip to content

Instantly share code, notes, and snippets.

@jasdeepkhalsa
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasdeepkhalsa/8860347 to your computer and use it in GitHub Desktop.
Save jasdeepkhalsa/8860347 to your computer and use it in GitHub Desktop.
Extracting first letters of each word in a string depending upon a RegEx in MySQL
update scriptures set search = replace(search,'॥','');
update scriptures set search = replace(search,'ਉ','ੳ');
update scriptures set search = replace(search,'ਊ','ੳ');
update scriptures set search = replace(search,'ਓ','ੳ');
update scriptures set search = replace(search,'ਔ','ਅ');
update scriptures set search = replace(search,'ਐ','ਅ');
update scriptures set search = replace(search,'ਆ','ਅ');
update scriptures set search = replace(search,'ਏ','ੲ');
update scriptures set search = replace(search,'ਈ','ੲ');
update scriptures set search = replace(search,'ਇ','ੲ');
update scriptures set search = replace(search,'੦','');
update scriptures set search = replace(search,'੧','');
update scriptures set search = replace(search,'੨','');
update scriptures set search = replace(search,'੩','');
update scriptures set search = replace(search,'੪','');
update scriptures set search = replace(search,'੫','');
update scriptures set search = replace(search,'੬','');
update scriptures set search = replace(search,'੭','');
update scriptures set search = replace(search,'੮','');
update scriptures set search = replace(search,'੯','');
delimiter $$
drop function if exists `initials`$$
CREATE FUNCTION `initials`(str text, expr text) RETURNS text CHARSET utf8
begin
declare result text default '';
declare buffer text default '';
declare i int default 1;
if(str is null) then
return null;
end if;
set buffer = trim(str);
while i <= length(buffer) do
if substr(buffer, i, 1) regexp expr then
set result = concat( result, substr( buffer, i, 1 ));
set i = i + 1;
while i <= length( buffer ) and substr(buffer, i, 1) regexp expr do
set i = i + 1;
end while;
while i <= length( buffer ) and substr(buffer, i, 1) not regexp expr do
set i = i + 1;
end while;
else
set i = i + 1;
end if;
end while;
return result;
end$$
drop function if exists `acronym`$$
CREATE FUNCTION `acronym`(str text) RETURNS text CHARSET utf8
begin
declare result text default '';
set result = initials( str, '[ੴਓੳਅੲਸਹਕਖਗਘਙਚਛਜਝਞਟਠਡਢਣਤਥਦਧਨਪਫਬਭਮਯਰਲਵੜਸ਼ਖ਼ਗ਼ਜ਼ਫ਼ਲ਼]' );
return result;
end$$
delimiter ;
UPDATE scriptures SET search = acronym(scripture)
@jasdeepkhalsa
Copy link
Author

Just to explain the last line:

  • scriptures is the table I want to update
  • search is a new empty column I created inside the table to store the result
  • scripture is an existing column inside the scriptures table with all the strings I want to extract from
  • acronym is the function previously declared which is looking to match the first letter of each word with a character from the RegEx [ੴਓੳਅੲਸਹਕਖਗਘਙਚਛਜਝਞਟਠਡਢਣਤਥਦਧਨਪਫਬਭਮਯਰਲਵੜਸ਼ਖ਼ਗ਼ਜ਼ਫ਼ਲ਼]

So this final line of the code will go through each row of the column scripture, apply the function acronym to it and store the result in the new search column.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment