Regex_replace仅检查1个实例

问题描述 投票:0回答:1

您好如何创建一个正则表达式,该模式应该只有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 ")
sql regex oracle replace plsql
1个回答
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在线查看。

© www.soinside.com 2019 - 2024. All rights reserved.