简体   繁体   中英

Regex, TextFSM - Match content between two characters while excluding unwanted list of strings

Hello Developer Community!

I'm currently working on developing some Ansible playbooks to manage Citrix NetScaler configuration and would like to ask for some help about the following.

I have the following configuration line what I would like to parse with TextFSM:

add lb vserver VS_ssl_443_testapps SSL 0.0.0.0 0 -persistenceType RULE -timeout 30 -rule "HTTP.REQ.COOKIE.VALUE(\"JSESSIONID\")" -resRule "HTTP.RES.SET_COOKIE.COOKIE(\"JSESSIONID\").VALUE(\"JSESSIONID\")" -cltTimeout 600 -td 1

I would need to write a regex matching any content between double quotes after the -rule keyword. The content between the start and end double quotes can contain more double quotes.

"HTTP.REQ.COOKIE.VALUE(\"JSESSIONID\")"

My problem is that, in case there is an optional -resRule keyword defined after the -rule keyword and the text after the -resRule keyword also contains double quotes, the content match includes contents after both -rule and -resRule keywords.

Is it possible to define a list of "unwanted" keywords when the content between the starting and ending double qoutes contains any of the keywords, the match ends at the last double qoute before the unwanted keyword?

So for example,

在此处输入图像描述

I'm trying to play with lookaheads and non-capture groups, but with no luck.

https://regex101.com/r/UkPr05/1

(((\")(.*)(\"))(?:( -resRule.*)))

Thank You very much in advance!

You can use the expression -rule\s*(\"(\\\"|[^\"])*\") that will take only the quoted value for the argument -rule , the key changes are

  1. The pattern starts with -rule ,
  2. the parts consumes a quote if it is escaped in the repeating group, and consume an additional final quote.

https://regex101.com/r/HoCM1v/1/

If you wanted to included unquoted parameters as well you could use -rule\s*(\"(\\\"|[^\"])*\"|\S+) , where the \S+ represents characters up to next space.

You can match -rule\s* followed by optional whitespace chars and "

Then match any char except " or match an escaped char followed by matching the closing " at the end do -resRule will not match part of the match.

-rule\s*"([^"\\]*(?:\\.[^"\\]*)*)"

Regex demo

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