我需要在 Oracle 中编写脚本,在查询中按/字符拆分字符串“BP/593/00294”和“NC//12345”,以便在单独的列中包含值。
我在想这样的事情:
select regexp_substr(mystr, '[^/]+') as col1,
regexp_substr(c.usertext21,'[^/]+',1,2) as col2
from mytable
但在 col2 中,我从第二个字符串中丢失了空字符串值。 我需要保留两个字符串中的每个值。这应该是结果:
<table>
<th>col1</th>
<th>col2</th>
<th>col3</th>
<tr>
<td>BP</td>
<td>593</td>
<td>00294</td>
</tr>
<tr>
<td>NC</td>
<td></td>
<td>12345</td>
</tr>
</table>
如有任何帮助,我们将不胜感激。 谢谢
最佳答案
您可以在字符串开头或 /
之后捕获除 /
之外的 0 个或多个字符:
select
regexp_substr('BP/593/00294', '(^|/)([^/]*)') as col1,
regexp_substr('BP/593/00294', '(^|/)([^/]*)', 1, 2, null, 2) as col2,
regexp_substr('BP/593/00294', '(^|/)([^/]*)', 1, 3, null, 2) as col3
from dual
请参阅online demo .
详细信息
(^|/)
- 捕获组 1:字符串开头或/
([^/]*)
- 捕获组 2:除/
之外的任何 0 个或多个字符。
请注意提取第 2 组值的 2
参数。请参阅regex demo .
关于regex - Oracle中使用正则表达式按分隔符分割字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54869830/