添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
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%"

Please give us an example of the output so we can have a better understanding of what you need – Rui Jarimba Nov 4, 2018 at 12:08 Perhaps without use the anchor ^ you could use (?<replace>(?<column>\[[^]]+\]\.\w+)\.Contains\("(?<like>%[^%]+\%)"\))(?: And | Or |$) Demo – The fourth bird Nov 4, 2018 at 12:13 You do not need regex : string[] matches = input.Split(new string[] { "And", "Or" }, StringSplitOptions.None).ToArray(); – jdweng Nov 4, 2018 at 12:35 I see what you mean, but if it can be solved with a single Regex, that would be preferable. – mfas Nov 4, 2018 at 12:37

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.