Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- select reg_replace2('---hhggh---hhj','^[^а-яa-z0-9]+','',TRUE,1,0)
- DELIMITER $$
- CREATE FUNCTION reg_replace2(
- subject VARCHAR(21845),
- pattern VARCHAR(21845),
- replacement VARCHAR(21845),greedy BOOLEAN, minMatchLen INT, maxMatchLen INT)
- RETURNS VARCHAR(21845) DETERMINISTIC BEGIN
- DECLARE result, subStr, usePattern VARCHAR(21845);
- DECLARE startPos, prevStartPos, startInc, len, lenInc INT;
- IF subject REGEXP pattern THEN
- SET result = '';
- -- Sanitize input parameter values
- SET minMatchLen = IF(minMatchLen < 1, 1, minMatchLen);
- SET maxMatchLen = IF(maxMatchLen < 1 OR maxMatchLen > CHAR_LENGTH(subject),
- CHAR_LENGTH(subject), maxMatchLen);
- -- Set the pattern to use to match an entire string rather than part of a string
- SET usePattern = IF (LEFT(pattern, 1) = '^', pattern, CONCAT('^', pattern));
- SET usePattern = IF (RIGHT(pattern, 1) = '$', usePattern, CONCAT(usePattern, '$'));
- -- Set start position to 1 if pattern starts with ^ or doesn't end with $.
- IF LEFT(pattern, 1) = '^' OR RIGHT(pattern, 1) <> '$' THEN
- SET startPos = 1, startInc = 1;
- -- Otherwise (i.e. pattern ends with $ but doesn't start with ^): Set start position
- -- to the min or max match length from the end (depending on "greedy" flag).
- ELSEIF greedy THEN
- SET startPos = CHAR_LENGTH(subject) - maxMatchLen + 1, startInc = 1;
- ELSE
- SET startPos = CHAR_LENGTH(subject) - minMatchLen + 1, startInc = -1;
- END IF;
- WHILE startPos >= 1 AND startPos <= CHAR_LENGTH(subject)
- AND startPos + minMatchLen - 1 <= CHAR_LENGTH(subject)
- AND !(LEFT(pattern, 1) = '^' AND startPos <> 1)
- AND !(RIGHT(pattern, 1) = '$'
- AND startPos + maxMatchLen - 1 < CHAR_LENGTH(subject)) DO
- -- Set start length to maximum if matching greedily or pattern ends with $.
- -- Otherwise set starting length to the minimum match length.
- IF greedy OR RIGHT(pattern, 1) = '$' THEN
- SET len = LEAST(CHAR_LENGTH(subject) - startPos + 1, maxMatchLen), lenInc = -1;
- ELSE
- SET len = minMatchLen, lenInc = 1;
- END IF;
- SET prevStartPos = startPos;
- lenLoop: WHILE len >= 1 AND len <= maxMatchLen
- AND startPos + len - 1 <= CHAR_LENGTH(subject)
- AND !(RIGHT(pattern, 1) = '$'
- AND startPos + len - 1 <> CHAR_LENGTH(subject)) DO
- SET subStr = SUBSTRING(subject, startPos, len);
- IF subStr REGEXP usePattern THEN
- SET result = IF(startInc = 1,
- CONCAT(result, replacement), CONCAT(replacement, result));
- SET startPos = startPos + startInc * len;
- LEAVE lenLoop;
- END IF;
- SET len = len + lenInc;
- END WHILE;
- IF (startPos = prevStartPos) THEN
- SET result = IF(startInc = 1, CONCAT(result, SUBSTRING(subject, startPos, 1)),
- CONCAT(SUBSTRING(subject, startPos, 1), result));
- SET startPos = startPos + startInc;
- END IF;
- END WHILE;
- IF startInc = 1 AND startPos <= CHAR_LENGTH(subject) THEN
- SET result = CONCAT(result, RIGHT(subject, CHAR_LENGTH(subject) + 1 - startPos));
- ELSEIF startInc = -1 AND startPos >= 1 THEN
- SET result = CONCAT(LEFT(subject, startPos), result);
- END IF;
- ELSE
- SET result = subject;
- END IF;
- RETURN result;
- END$$
- DELIMITER ;
- DROP FUNCTION IF EXISTS reg_replace2;
- SELECT txt,
- reg_replace(txt,
- 'q.*k b.*n',
- 'slow red',
- TRUE,
- 5,
- 0
- ) AS test1,
- reg_replace(txt,
- '[Tt][^ ]* ',
- 'a ',
- TRUE,
- 2,
- 0
- ) AS test2,
- reg_replace(txt,
- '^[Tt][^ ]* ',
- 'a ',
- TRUE,
- 2,
- 0
- ) AS test3,
- reg_replace(txt,
- 'd.*g$',
- 'cat',
- TRUE,
- 2,
- 0
- ) AS test4,
- reg_replace(txt,
- '[[:space:]]+', -- See http://stackoverflow.com/questions/15558172#15558268
- '_',
- TRUE,
- 1,
- 0
- ) AS test5,
- reg_replace(txt,
- 'l.+',
- 'log',
- TRUE,
- 2,
- 0
- ) AS test6,
- reg_replace(txt,
- '^Th.*og$',
- 'No foxes or dogs here!',
- TRUE,
- 4,
- 0
- ) AS test7
- FROM test;
- DELIMITER $$
- CREATE FUNCTION `regex_replace`(pattern VARCHAR(1000),replacement VARCHAR(1000),original VARCHAR(1000))
- RETURNS VARCHAR(1000)
- DETERMINISTIC
- BEGIN
- DECLARE temp VARCHAR(1000);
- DECLARE ch VARCHAR(1);
- DECLARE i INT;
- SET i = 1;
- SET temp = '';
- IF original REGEXP pattern THEN
- loop_label: LOOP
- IF i>CHAR_LENGTH(original) THEN
- LEAVE loop_label;
- END IF;
- SET ch = SUBSTRING(original,i,1);
- IF NOT ch REGEXP pattern THEN
- SET temp = CONCAT(temp,ch);
- ELSE
- SET temp = CONCAT(temp,replacement);
- END IF;
- SET i=i+1;
- END LOOP;
- ELSE
- SET temp = original;
- END IF;
- RETURN temp;
- END$$
- DELIMITER ;
- select regex_replace('[^a-zA-Z0-9\-]','','2my test3_text-to. check \\ my- sql (regular) ,expressions ._,');
- Demo http://sqlfiddle.com/#!9/5e5c1/1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement