简体   繁体   中英

Regex - how to capture text in quotes within parentheses

I am using http://www.rubular.com/ to test how much pattern matching will work. If I have text such as AND TO_CHARCREATE_TS, 'yyyymmdd' = '20140810' , the regex AND TO_CHARCREATE_TS, yyyymmdd = '(.*?)' allows me to extract the time stamp. However, when parenthesis are introduced such as AND (TO_CHAR(CREATE_TS, 'yyyymmdd') = '20140810') , I cannot get my regex AND (TO_CHAR(CREATE_TS, 'yyyymmdd') = '(.*?)' ) to extract the time stamp. Any help is appreciated

If the Parenthesis are in the actual string you are trying to match, they need to be escaped with '\\' for it to match.

AND \(TO_CHAR\(CREATE_TS, 'yyyymmdd'\) = '(.*?)'\)

Will match the example

AND (TO_CHAR(CREATE_TS, 'yyyymmdd') = '20140810')

Also, depending on what else is going on in the query that you are matching, you may be able to simplify the regex to grab just a quoted timestamp like this:

'(\d{8})'

Which will match any 8 digits inside of single-quotes

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