2013-07-21 53 views
0

你能解釋一下爲什麼我從這三個正則表達式中得到不同的結果嗎?我期待所有三人的第一次輸出。regexp_substr結果解釋

SQL> select regexp_substr(input,'.*') sub from test_regexp; 
SUB 
------------------------------------------------------------------------------ 
Understanding greediness, not the Enron kind 

SQL> select regexp_substr(input,'[A-Za-z ]*') sub from test_regexp; 
SUB 
------------------------------------------------------------------------------ 
Understanding greediness 

SQL> select regexp_substr(input,'[[:alpha:][:space:]]*') sub from test_regexp; 
SUB 
------------------------------------------------------------------------------ 
Understanding greediness 

感謝

回答

1

.*將匹配任何字符存在的零到無窮倍。

[A-Za-z ]*將匹配字符A-Z,a-z和空格而不是「」,「」。正則表達式只會匹配逗號前面的部分,因爲它(會貪婪地)匹配與正則表達式匹配的第一個匹配。

[[:alpha:][:space:]]*將匹配字母字符(A-Z & a-z)和空格字符。這意味着它將與上面的匹配,但它也將匹配製表符和換行符等。

如果您想在包含逗號和文本後可以簡單地將,添加到字符類中。

[A-Za-z, ]* 
+0

感謝您的完整的解釋和正確的解決方案。我錯過了匹配','的表情。 – pm08