regex - Oracle中使用正则表达式按分隔符分割字符串

标签 regex oracle plsql

我需要在 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

enter image description here

请参阅online demo .

详细信息

  • (^|/) - 捕获组 1:字符串开头或 /
  • ([^/]*) - 捕获组 2:除 / 之外的任何 0 个或多个字符。

请注意提取第 2 组值的 2 参数。请参阅regex demo .

关于regex - Oracle中使用正则表达式按分隔符分割字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54869830/

相关文章:

JavaScript - 为什么分割不断返回空元素?

javascript - regex101 条件语句总是 else

Python ETL - 使用 cx_Oracle 批量或迭代地将大型数据集加载到 Oracle 数据库中

java - 如何将游标或记录列表传递给pl/sql中的java过程?

r - 在 dplyr select 中使用 perl=TRUE regex

sql - 具有动态参数的Oracle Lag函数

database - 我应该对表进行分区/子分区吗?

oracle - 在Oracle中,PARALLEL被广泛使用。 PARALLEL、PARALLEL(8)、PARALLEL(a,8) 之间有什么区别?

sql - SQL中是否存在 "LIKE"和 "IN"的组合?

java - (?i) 正则表达式结果 - "Nothing"被 Something 取代