我在為此尋找解決方案時遇到問題
set serveroutput on;
declare
EmailRegexp CONSTANT VARCHAR2(1000) :='^[a-z0-9!#$%&''* /=?^_`{|}~-] (\.[a-z0-9!#$%&''* /=?^_`{|}~-] )*@([a-z0-9]([a-z0-9-]*[a-z0-9])?\.) ([A-Z]{2,})$';
p_vInEmailAddress VARCHAR2(30) := '[email protected]';
begin
dbms_output.put_line('regex: '||LOWER(SUBSTR(REGEXP_SUBSTR(p_vInEmailAddress, '\.([^.\n\s]*)$'), 2))||'''');
end;
--results:
--regex: '
--PL/SQL procedure successfully completed.
--now if i do this instead:
p_vInEmailAddress VARCHAR2(30) := '[email protected]';
--I get results back
--PL/SQL procedure successfully completed.
--regex: com'
我做錯了什么,為什么它不喜歡“.net”部分?
我怎樣才能解決這個問題?
謝謝
uj5u.com熱心網友回復:
查詢的問題部分可以簡化為:
begin
dbms_output.put_line(
REGEXP_SUBSTR(
'[email protected]',
'\.([^.\n\s]*)$'
)
);
end;
/
正則運算式\.([^.\n\s]*)$正在尋找:
- 一個點字符
\.;然后 - 零個或多個不是點
.、斜線\或n斜線\或斜線的字符s;然后 - 字串的結尾。
問題是您的字串有一個n字符,而正則運算式排除nas\n被解釋為兩個字符,而不是表示單個換行符的類似 perl 的運算式。您想\n用CHR(10)字串文字外部的字符(或其中的換行符)和\sposix-expression 替換[:space:]。
你想要的是:
begin
dbms_output.put_line(
REGEXP_SUBSTR(
'[email protected]',
'\.([^.' || CHR(10) || '[:space:]]*)$'
)
);
end;
/
或者
begin
dbms_output.put_line(
REGEXP_SUBSTR(
'[email protected]',
'\.([^.
[:space:]]*)$'
)
);
end;
/
db<>在這里擺弄
uj5u.com熱心網友回復:
只需使用
REGEXP_SUBSTR(p_vInEmailAddress,'\.([^.[:space:]]*)$')
\s 不匹配 Oracle 正則運算式中字符類中的空格。
[:space:] 匹配水平和垂直空白。
解釋
--------------------------------------------------------------------------------
\. '.'
--------------------------------------------------------------------------------
( group and capture to \1:
--------------------------------------------------------------------------------
[^.[:space:]]* any character except: '.', whitespace
characters (like \s) (0 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
) end of \1
--------------------------------------------------------------------------------
$ before an optional \n, and the end of the
string
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/391952.html
