지정된 패턴(또는 패턴의 모든 발생 항목)이 제거되거나 대체 문자열로 바뀐 대상 문자열을 반환합니다.
예
다음 예는 문자열의 모든 공백을 없는 것으로 바꿉니다(즉, 모든 공백이 제거됨).
SELECT REGEXP_REPLACE('It was the best of times, it was the worst of times',
'( ){1,}',
'') AS result;
+------------------------------------------+
| RESULT |
|------------------------------------------|
| Itwasthebestoftimes,itwastheworstoftimes |
+------------------------------------------+
다음 예는 문자열 times 와 일치하며, 이를 문자열 days 로 바꿉니다. 일치는 문자열의 첫 번째 문자에서 시작하며, 하위 문자열의 두 번째 발생 항목을 바꿉니다.
SELECT REGEXP_REPLACE('It was the best of times, it was the worst of times',
'times',
'days',
1,
2) AS result;
+----------------------------------------------------+
| RESULT |
|----------------------------------------------------|
| It was the best of times, it was the worst of days |
+----------------------------------------------------+
다음 예는 역참조를 사용하여 문자열 firstname middlename lastname 을 lastname, firstname middlename 으로 재정렬하고 lastname 및 firstname 사이에 쉼표를 삽입합니다.
SELECT REGEXP_REPLACE('firstname middlename lastname',
'(.*) (.*) (.*)',
'\\3, \\1 \\2') AS name_sort;
+--------------------------------+
| NAME_SORT |
|--------------------------------|
| lastname, firstname middlename |
+--------------------------------+
나머지 예제에서는 다음 테이블의 데이터를 사용합니다.
CREATE OR REPLACE TABLE regexp_replace_demo(body VARCHAR(255));
INSERT INTO regexp_replace_demo values
('Hellooo World'),
('How are you doing today?'),
('the quick brown fox jumps over the lazy dog'),
('PACK MY BOX WITH FIVE DOZEN LIQUOR JUGS');
다음 예에서는 빈 그룹(())을 통해 시작과 끝을 포함하여 대상 문자열의 모든 문자 사이에 * 문자를 삽입하며, 이 그룹은 두 문자 사이의 일치 항목을 찾습니다.
SELECT body,
REGEXP_REPLACE(body, '()', '*') AS replaced
FROM regexp_replace_demo;
+---------------------------------------------+-----------------------------------------------------------------------------------------+
| BODY | REPLACED |
|---------------------------------------------+-----------------------------------------------------------------------------------------|
| Hellooo World | *H*e*l*l*o*o*o* *W*o*r*l*d* |
| How are you doing today? | *H*o*w* *a*r*e* *y*o*u* *d*o*i*n*g* *t*o*d*a*y*?* |
| the quick brown fox jumps over the lazy dog | *t*h*e* *q*u*i*c*k* *b*r*o*w*n* *f*o*x* *j*u*m*p*s* *o*v*e*r* *t*h*e* *l*a*z*y* *d*o*g* |
| PACK MY BOX WITH FIVE DOZEN LIQUOR JUGS | *P*A*C*K* *M*Y* *B*O*X* *W*I*T*H* *F*I*V*E* *D*O*Z*E*N* *L*I*Q*U*O*R* *J*U*G*S* |
+---------------------------------------------+-----------------------------------------------------------------------------------------+
다음 예에서는 순서나 대/소문자에 관계없이 모든 모음을 아무것도 바꾸지 않고 모두 제거합니다.
SELECT body,
REGEXP_REPLACE(body, '[aeiou]', '', 1, 0, 'i') AS replaced
FROM regexp_replace_demo;
+---------------------------------------------+----------------------------------+
| BODY | REPLACED |
|---------------------------------------------+----------------------------------|
| Hellooo World | Hll Wrld |
| How are you doing today? | Hw r y dng tdy? |
| the quick brown fox jumps over the lazy dog | th qck brwn fx jmps vr th lzy dg |
| PACK MY BOX WITH FIVE DOZEN LIQUOR JUGS | PCK MY BX WTH FV DZN LQR JGS |
+---------------------------------------------+----------------------------------+
다음 예에서는 단어 경계(\b)와 일치시킨 후, 0개 이상의 단어 문자(\S), 문자 o, 그리고 다음 단어 경계까지 0개 이상의 단어 문자를 일치시켜 대상 문자열에서 소문자 :code:`o`을 포함하는 모든 단어를 제거합니다.
SELECT body,
REGEXP_REPLACE(body, '\\b(\\S*)o(\\S*)\\b') AS replaced
FROM regexp_replace_demo;
+---------------------------------------------+-----------------------------------------+
| BODY | REPLACED |
|---------------------------------------------+-----------------------------------------|
| Hellooo World | |
| How are you doing today? | are ? |
| the quick brown fox jumps over the lazy dog | the quick jumps the lazy |
| PACK MY BOX WITH FIVE DOZEN LIQUOR JUGS | PACK MY BOX WITH FIVE DOZEN LIQUOR JUGS |
+---------------------------------------------+-----------------------------------------+
다음 예에서는 소문자 o`를 포함하는 모든 단어를 바꾸고, 첫 번째 ``o` 앞뒤에 있는 문자를 바꾸고, o`를 :code:`@@ 문자 시퀀스로 바꿉니다.
SELECT body,
REGEXP_REPLACE(body, '\\b(\\S*)o(\\S*)\\b', '\\2@@\\1') AS replaced
FROM regexp_replace_demo;
+---------------------------------------------+-------------------------------------------------+
| BODY | REPLACED |
|---------------------------------------------+-------------------------------------------------|
| Hellooo World | @@Helloo rld@@W |
| How are you doing today? | w@@H are u@@y ing@@d day@@t? |
| the quick brown fox jumps over the lazy dog | the quick wn@@br x@@f jumps ver@@ the lazy g@@d |
| PACK MY BOX WITH FIVE DOZEN LIQUOR JUGS | PACK MY BOX WITH FIVE DOZEN LIQUOR JUGS |
+---------------------------------------------+-------------------------------------------------+
다음 예는 이전 예와 동일하지만, 교체는 대상 문자열의 3 위치에서 시작됩니다.
SELECT body,
REGEXP_REPLACE(body, '\\b(\\S*)o(\\S*)\\b', '\\2@@\\1', 3) AS replaced
FROM regexp_replace_demo;
+---------------------------------------------+-------------------------------------------------+
| BODY | REPLACED |
|---------------------------------------------+-------------------------------------------------|
| Hellooo World | He@@lloo rld@@W |
| How are you doing today? | How are u@@y ing@@d day@@t? |
| the quick brown fox jumps over the lazy dog | the quick wn@@br x@@f jumps ver@@ the lazy g@@d |
| PACK MY BOX WITH FIVE DOZEN LIQUOR JUGS | PACK MY BOX WITH FIVE DOZEN LIQUOR JUGS |
+---------------------------------------------+-------------------------------------------------+
다음 예는 이전 예와 동일하지만, 대상 문자열의 3 위치에서 시작하여 세 번째 발생만 교체됩니다.
SELECT body,
REGEXP_REPLACE(body, '\\b(\\S*)o(\\S*)\\b', '\\2@@\\1', 3, 3) AS replaced
FROM regexp_replace_demo;
+---------------------------------------------+----------------------------------------------+
| BODY | REPLACED |
|---------------------------------------------+----------------------------------------------|
| Hellooo World | Hellooo World |
| How are you doing today? | How are you doing day@@t? |
| the quick brown fox jumps over the lazy dog | the quick brown fox jumps ver@@ the lazy dog |
| PACK MY BOX WITH FIVE DOZEN LIQUOR JUGS | PACK MY BOX WITH FIVE DOZEN LIQUOR JUGS |
+---------------------------------------------+----------------------------------------------+
다음 예는 이전 예와 동일하지만, 대/소문자를 구분하지 않는 일치를 사용합니다.
SELECT body,
REGEXP_REPLACE(body, '\\b(\\S*)o(\\S*)\\b', '\\2@@\\1', 3, 3, 'i') AS replaced
FROM regexp_replace_demo;
+---------------------------------------------+----------------------------------------------+
| BODY | REPLACED |
|---------------------------------------------+----------------------------------------------|
| Hellooo World | Hellooo World |
| How are you doing today? | How are you doing day@@t? |
| the quick brown fox jumps over the lazy dog | the quick brown fox jumps ver@@ the lazy dog |
| PACK MY BOX WITH FIVE DOZEN LIQUOR JUGS | PACK MY BOX WITH FIVE DOZEN R@@LIQU JUGS |
+---------------------------------------------+----------------------------------------------+