Regex_replace仅检查1个实例

时间:2019-03-08 06:25:18

标签: sql regex oracle replace plsql

您好如何创建一个正则表达式,其模式应该只有1个“

实例

例如:

1. STRING" 
2. "STRING
3. STRI"NG
4. STRING""
5. "STRING"

结果:

1. STRING""
2. ""STRING
3. STRI""NG
4. STRING"" (No change since 2 instance of ")
5. "STRING"   (No change since 2 instance of ")

1 个答案:

答案 0 :(得分:4)

类似这样的东西:

with t as (
  select 'STRING"' as s from dual union all
  select '"STRING' from dual union all
  select 'STRI"NG' from dual union all
  select 'STRING""' from dual union all
  select '"STRING"' from dual union all
  select 'STRING' from dual union all
  select '"STR"ING"' from dual union all
  select '"' from dual union all
  select '""' from dual union all
  select '"""' from dual
)
select
  s,
  regexp_replace(s,'^([^"]*)"([^"]*)$', '\1""\2') new_s
from t

输出:

+-----------+-----------+
|     S     |   NEW_S   |
+-----------+-----------+
| STRING"   | STRING""  |
| "STRING   | ""STRING  |
| STRI"NG   | STRI""NG  |
| STRING""  | STRING""  |
| "STRING"  | "STRING"  |
| STRING    | STRING    |
| "STR"ING" | "STR"ING" |
| "         | ""        |
| ""        | ""        |
| """       | """       |
+-----------+-----------+

说明:

^       # asserts position at start of a line
(       # 1st capturing group
 [^"]*  # matches characters that are not the character ", zero or more times
)
"       # matches the character " literally
(       # 2nd capturing group
 [^"]*
)
$       # asserts position at the end of a line

通过rextester在线检查。