简体   繁体   中英

how to find and replace inbetween something with regex in a mysql trigger

I have linked 4 chats from 4 different forums databases with triggers, and now I want to make command to trigger an anonymous announcement my typing "/msg message here"

Here is an example messages as they are in the table column

(AGN)[color=#EEFD01]this is a message example[/color]
(ND)[color=#1EB101]this is a message example[/color]

the letters in round brackets at the beginning represent what forum the message is coming from.

here is my trigger so far :

CREATE TRIGGER agn_sign_chat BEFORE INSERT ON dark_taigachat FOR EACH ROW BEGIN
IF (INSTR(NEW.message,"/msg") > 0) THEN
SET NEW.message = CONCAT("(!) ", NEW.message);
SET NEW.message = REPLACE(NEW.message,"/msg ","");
SET NEW.username = "ANNOUNCEMENT";
SET NEW.user_id = 996;
//HERE I WANT TO REPLACE COLOR TAG IN MESSAGE TO #fd0101 (RED)
ELSEIF (INSTR(NEW.message,"(") != 1) THEN
SET NEW.message = CONCAT("(AGN) ", NEW.message);
END IF ;
END ; 

The ElSEIF is just handling the forum sign, its under the IF that I am changing the message details to look like a general announcement message.

It works but keeps the color of the font to my person font color so I want to swap it with a red color like #fd0101. If I new the font color of the person using this /msg command, i could use RePLACE() to swap his color with red but the color value will vary, So im guessing i need to use REGEX, i have used a few times to select things in mysql databases but never used in a trigger. Is it possible to do this? maybe something like this will select the color #([0-9]|[AF]) but how to find and replace that?

thank you for your time

Try /(color=#\\w*)/g regex and replace it with proper color eg color=#fd0101

Good to use http://regexr.com/3aov5 to check

mark it as answer if it helps :)

Sorry, there is no regexp replace capability in MySQL.

You need to do that in some client language (PHP, Java, etc).

SET NEW.message = REGEXP_REPLACE(NEW.message, '\\#([a-fA-F]|[0-9]){3,6}', '\\#fd0101')

In theory, it should work. Double escaping "\\\\" is required at least for PhpMyAdmin.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM