2013-03-28 59 views
0

嗨我有一些SQL代碼,我試圖解析並提取一些文本。正則表達式提取子串

CREATE OR REPLACE VIEW the_view_name as 

我想REPLACE VIEW並且不包括開頭或結尾的空格AS之間的文本。

目前我收到了很多與/REPLACE\s*VIEW(.*?)\s*AS/gi

感謝

回答

3

試着這樣做:

echo 'CREATE OR REPLACE VIEW the_view_name as' | 
    perl -lne '/replace\s+view\s+(.*?)\s+as\b/i && print "[$1]"' 
[the_view_name] 

您可以使用此正則表達式太你的情況:

/replace\s+view\s+(\S+)\s+as\b/i 

perldoc perlrebackslash

\S : Character class for non whitespace 
+0

$ sqlline =〜/replace\s+view\s+(.*?)\s+as \雙; $ viewname = $ 1; print「viewname:$ viewname \ n」; – jaybee

+0

呃?你什麼意思 ? –

+1

一切都好;這就是我最終使用的。我只需要1美元 – jaybee

0

你目前缺少視圖後的空間,如果你想除了空間的一切,然後你捕獲組應該是([^\s]*),這將收集的一切,直到一個空間。

+0

感謝;更換\ S * VIEW \ S * ([^ \ s] *)給我替換視圖the_view_name我仍然只有'the_view_name' – jaybee