0

Hi I have some SQL code that I am trying to parse and extract some text.

CREATE OR REPLACE VIEW the_view_name as

I want the text between REPLACE VIEW and the AS which does not include leading or trailing spaces.

At the moment I'm getting the whole lot with /REPLACE\s*VIEW(.*?)\s*AS/gi.

Thanks

2 Answers 2

3

Try doing this :

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

You can use this regex too in your case :

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

See perldoc perlrebackslash

\S : Character class for non whitespace
Sign up to request clarification or add additional context in comments.

1 Comment

$sqlline =~ /replace\s+view\s+(.*?)\s+as\b/i; $viewname = $1; print "viewname : $viewname\n";
0

You're currently missing a space after VIEW, and if you want everything except spaces then your capture group should be ([^\s]*), which will collect everything up until a space.

1 Comment

Thanks;REPLACE\sVIEW\s*([^\s]) gives me REPLACE VIEW the_view_name I'm still after only 'the_view_name'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.