1

I would like to match different things, and add condition that these matches must be between specific boundaries,

This is the text

protocol SceneDisplayView {
    func displayEmailScreen()
    func displayPasswordScreen()
    func displayNameScreen()
}

What i did so far is find my matches, using this regex:

(?:^\s*func\s(display\w*)\(\)$)

This will end as the following : enter image description here

as seen here in the demo: https://regex101.com/r/IuQ7nO/1/

I'm trying to limit these matches that they must be between protocol ... { and closing bracket }

so that if there's any floating match outside of these boundaries, they would not show up, how could this be achieved with one regex ?

Osa
  • 1,922
  • 7
  • 30
  • 51
  • Not sure if it is supported, but perhaps `(?:\G(?!^)|\bprotocol\h+\S+\h*{)\R\h*\Kfunc\h+(display\w*)\(\)(?=[^{}]*\})` https://regex101.com/r/8QU8qf/1 – The fourth bird Dec 16 '20 at 00:09
  • 1
    Or if only `\G` is supported using 2 capturing groups `(?:\G(?!^)|\bprotocol\s+\S+\s*{)\r?\n\s*(func\s+(display\w*)\(\))(?=[^{}]*\})` https://regex101.com/r/5AnKJC/1 – The fourth bird Dec 16 '20 at 00:18
  • @Thefourthbird That almost did it, but there's a small problem, if the first func after protocol bracket does not start with display, it will fail, although there are other func that start with display, between protocol starting and ending brackets, failing example: https://regex101.com/r/9xmIxv/1 – Osa Dec 16 '20 at 00:49
  • Then you can add matching optional word characters before https://regex101.com/r/OC4cNQ/1 I am not sure `\R` and `\K` are supported in swiftlint. Else you might try https://regex101.com/r/nUpMco/1 – The fourth bird Dec 16 '20 at 00:50
  • Did that work out? – The fourth bird Dec 16 '20 at 01:22
  • @Thefourthbird Apparently only \K is not supported in swiftlint, however, i want to only match func that start with 'display', no matter where they are placed between the brackets,the issue with the current regex is that it always interferes with other func's in other order, failing examples: https://regex101.com/r/y8oIG3/1 – Osa Dec 16 '20 at 01:22
  • Try it like this, matching the ones that do not start with display, and capture the ones that do https://regex101.com/r/saaZiK/1 – The fourth bird Dec 16 '20 at 01:27
  • @Thefourthbird Swiftlint will catch all matches, there's no option to define which group is the target unfortunately. (including first line which is protocol ..., is also considered as a match and it gets linted) – Osa Dec 16 '20 at 01:30

0 Answers0