简体   繁体   中英

Regular expression to extract all words starting with colon

I would like to use a regular expression to extract "bind variable" parameters from a string that contains a SQL statement. In Oracle, the parameters are prefixed with a colon.

For example, like this:

SELECT * FROM employee WHERE name = :variable1 OR empno = :variable2

Can I use a regular expression to extract "variable1" and "variable2" from the string? That is, get all words that start with colon and end with space, comma, or the end of the string.

(I don't care if I get the same name multiple times if the same variable has been used several times in the SQL statement; I can sort that out later.)

This might work:

:\w+

This just means "a colon, followed by one or more word-class characters ".

This obviously assumes you have a POSIX-compliant regular expression system, that supports the word-class syntax.

Of course, this only matches a single such reference. To get both, and skip the noise, something like this should work:

(:\w+).+(:\w+)

For being able to handle such an easy case by yourself you should have a look at regex quickstart .

For the meantime use:

:\w+

如果您的正则表达式解析器支持单词边界,

:[a-zA-Z_0-9]\b

Try the following:

sed -e 's/[ ,]/\\n/g' yourFile.sql | grep '^:.*$' | sort | uniq

assuming your SQL is in a file called "yourFile.sql".

This should give a list of variables with no duplicates.

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