Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
Example inputs:
[Table1].Name.Contains("%Test%") And ([Table1].Name.Contains("%Test2%") Or [Table1].Name.Contains("%Test3%"))
([Table1].Name.Contains("%Test%") Or [Table1].Name.Contains("%Test2%")) And [Table1].Name.Contains("%Test3%")
The different parts between the "And" and "Or" should be considered as the main text to be identified, and all other text such as parentheses should be ignored. I have 3 identifiable string I would like to parse in this example, but there could be none, or many more, and they could be enclosed in multiple or no parentheses.
So in the examples above I would need to identify 3 strings:
[Table1].Name.Contains("%Test%")
[Table1].Name.Contains("%Test2%")
[Table1].Name.Contains("%Test3%")
Regex so far:
^(?<replace>(?<column>.*?)(?:\.Contains\("(?<like>.*?)"\)))(.*)$
So far I can only capture the first string until the "And" but I would also like to capture to two last strings. I have been looking into positive/negative lookahead/lookbehind but I can't nail it.
Match information from https://regex101.com/
Match 1
Full match 0-107 `[Table1].Name.Contains("%Test%") And [Table1].Name.Contains("%Test2%") Or [Table1].Name.Contains("%Test3%")`
Group `replace` 0-32 `[Table1].Name.Contains("%Test%")`
Group `column` 0-13 `[Table1].Name`
Group `like` 24-30 `%Test%`
Group 4. 32-107 ` And [Table1].Name.Contains("%Test2%") Or [Table1].Name.Contains("%Test3%")`
For the input above, I would like to have 3 "replace" groups, 3 "column" groups and 3 "like" groups if that is possible.
So for output I would like 3 matches with each 3 groups, so:
Match 1, would have replace="[Table1].Name.Contains("%Test%")", column="[Table1].Name" and replace="%Test%"
Match 2, would have replace="[Table1].Name.Contains("%Test2%")", column="[Table1].Name" and replace="%Test2%"
Match 3, would have replace="[Table1].Name.Contains("%Test3%")", column="[Table1].Name" and replace="%Test3%"
–
–
–
–
As per the comments, for the first posted example data, you could omit the anchor ^
and use the 3 capturing groups:
(?<replace>(?<column>\[[^]]+\]\.\w+)\.Contains\("(?<like>%[^%]+\%)"\))(?: And | Or |$)
Explanation
(?<replace>
Start group replace
(?<column>
Start group column
\[[^]]+\]
Match opening [
, 1+ times not closing ]
and then closing ]
\.\w+
Match a dot and 1+ word characters
)
Closing group column
\.Contains\("
Match .contains("
(?<like>)
Start group like
%[^%]+\%
Match %
, 1+ times not %
and then %`
)
Close group like
"\)
Match ")
)
Close group replace
(?: And | Or |$)
Alternation which matches either And
, Or
or the end of the string
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.