我有一MID: 124281-2 - SID: 31701
列的字符串格式。
需要从中提取124281-2
和提取31701
。
试过
SELECT REGEXP_SUBSTR('MID: 124281-2 - SID: 31701', ':[^,]+-') FROM DUAL;
但结果是: 124281-2 -
如何删除:
and -
?
提前致谢
有些人在遇到问题时会想“我知道,我会使用正则表达式”。现在他们有两个问题。——杰米·扎温斯基
这并不是说永远不要使用正则表达式,但在这种情况下,可能会有一个更简单的选择;您可以尝试使用更简单的方法吗?例如,SUBSTR 字符 6 到 13 (124281-2) 和字符 22 到 26 (31701)?这是假设您可能没有固定的子字符串长度 - 但值得一看。
或者,如果您必须使用正则表达式,则需要使用组来捕获您想要返回的两个值;尝试更具体 - 比如[A-Z]+: ([0-9\-]+) \- [A-Z]+: ([0-9]+)
从 :
'中间:124281-2 - SID:31701'
仅获取 124281-2 的正则表达式是:
[0-9]{1,6}-[0-9]
描述:
[0-9]{1,6} match a single character present in the list below
Quantifier: Between 1 and 6 times, as many times as possible, giving back as
needed.
0-9 a single character in the range between 0 and 9
- matches the character - literally
[0-9] match a single character present in the list below
如果所需字符的限制介于“:”和“-”之间,则可以使用以下正则表达式:
(?<=:\s)[^,]+(?=\s-)
描述:
(?<=:\s) Positive Lookbehind - Assert that the regex below can be matched
: matches the character : literally
\s match any white space character [\r\n\t\f ]
[^,]+ match a single character not present in the list below
Quantifier: Between one and unlimited times, as many times as possible,
giving back as needed.
, the literal character ,
(?=\s-) Positive Lookahead - Assert that the regex below can be matched
\s match any white space character [\r\n\t\f ]
- matches the character - literally
希望这可以帮助。