例
次の例では、文字列内のすべてのスペースをスペース無しに置き換えます(つまり、すべてのスペースが削除されます)。
SELECT REGEXP_REPLACE('It was the best of times, it was the worst of times',
'( ){1,}',
'') AS result;
+------------------------------------------+
| RESULT |
|------------------------------------------|
| Itwasthebestoftimes,itwastheworstoftimes |
+------------------------------------------+
次の例では、文字列 times をマッチングし、文字列 days に置き換えます。マッチングは、文字列内の最初の文字から始まり、部分文字列の2番目の出現を置き換えます。
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');
次の例では、任意の2文字間の一致を検索する空のグループ(())を使用して、開始と終了を含む、対象のすべての文字間に文字``*``を挿入します。
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``の前後の文字の交換、:code:`o`の文字シーケンス:code:`@@`への置き換えによって、小文字の:code:`o`を含むすべての単語を置き換えます。
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``から始まり、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 |
+---------------------------------------------+----------------------------------------------+