I want to extract sub string from string using regular expression ie. the following input/out list shows what i really want. i need to extract the dept filter conditions from a string. Here the word [dept] is constant.so what the kind of regular expression is useful to extract the sub string in this scenario
Input------------------------------------------------Output
Some conditions And [dept]=IT [dept]=IT
Some conditions And [dept]=IT Or [dept]=Account [dept]=IT Or [dept]=Account
Some conditions And [dept] IN ('IT','Account') [dept] IN ('IT','Account')
[dept]=IT And some conditions [dept]=IT
[dept]=IT Or [dept]=Account And some conditions [dept]=IT Or [dept]=Account
[dept] IN ('IT','Account') And some conditions [dept] IN ('IT','Account')
This might be close to what you are after
(\[dept\]=\w+)( (Or)|(And))?|(\[dept\] IN \(.+?\))
It matches on your sample input like below, grouped in ().
([dept]=IT)
([dept]=IT Or) ([dept]=Account)
([dept] IN ('IT','Account'))
In your script you can join the groups on each line, ie., join ([dept]=IT Or) ([dept]=Account)
But if like suggested in the comments you are indeed parsing SQL, there are SQL parsers that will give you accurate access to your query WHERE expression.
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.