简体   繁体   中英

Oracle Scripts parsing using regex

I am trying to parse the oracle scripts. The purpose is to extract the number of functions, procedures, Loops, Exceptions etc. What is the most suitable way to accomplish this? I am currently doing this with regex. But it seems that it is not covering all the scenarios. I need some alternative for this.

I have created a following regex expression to find out the Packages, functions, procedures, loops, exceptions, metadata definitions, and anonymous blocks.

"create\\s+(or\\s+replace\\s+)?(package)\\s+(body\\s+)?([\\w\\.]+)" +//1-4
        "|(\\bcreate\\s+)?(\\bor\\s+)?(\\breplace\\s+)?(\\bfunction|\\bprocedure)\\s+(\\w+)" +//5-9
        "|(\\bexception)\\s" +//10
        "|(\\bcursor)\\s*(\\w+)"+//11-12
        "|(\\bloop)\\s"+//13
        "|(\\w+)\\s*([\\w\\.]+)\\s*%(ROW)?(TYPE);"+//14-17
        "|(\\bdeclare)\\s"//18

The technique is to eliminate the comments, and string literals and then apply the regex. It is working fine for me.

Its an interesting topic to do , you can use different purser . One popular one is ANTLR . Grammar for pl/sql is defined there.

You can also take a look This Answer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM