Reputation: 1559
Full text:
def A
pod 'Xyz', '0.1.0'
pod 'zdb', '1.1.0+beta'
pod 'Aero', '3.1.0'
end
def B
A
pod 'qq', '0.1.1'
pod 'publ', '1.1.0'
end
I want a function, the input value would be 'A' or 'B', the return value would be the multi lines. The pod name's character may be anything.
For example, If I input 'A', the result would be
pod 'Xyz', '0.1.0'
pod 'zdb', '1.1.0+beta'
pod 'Aero', '3.1.0'
If I input 'B', the result would be
pod 'Xyz', '0.1.0'
pod 'zdb', '1.1.0+beta'
pod 'Aero', '3.1.0'
pod 'qq', '0.1.1'
pod 'publ', '1.1.0'
How can I match this text?
Upvotes: 0
Views: 67
Reputation: 1665
It can be done using regex. I have not used ruby before so I will not be able to share any code. I did check few posts online to help me writing an answer.
"(?<=def [A-#{input}]\n)[^p]*(pod '.*?')(?=\nend)"gs
here #{input}
is your input variable. Please note that the regex must be applied globally and in single line mode (dot matches new line character). You have to do that in ruby. As I already mentioned, I can not do it.
I tried manually changing the input and testing it against your input string.
input 1:
A
match 1-group 1:
pod 'Xyz', '0.1.0'
pod 'zdb', '1.1.0+beta'
pod 'Aero', '3.1.0'
input 2:
B
match 1-group 1:
pod 'A-a', '0.1.0'
pod 'A-b', '1.1.0'
pod 'A-c', '3.1.0'
match 2-group 1:
pod 'B-a', '0.1.1'
pod 'B-b', '1.1.0'
As you can see, if applied globally, all the lines you are interested in will be obtained in group 1 will some additional spaces which can always be eliminated.
Test link: https://regex101.com/r/AljaZP/1
Upvotes: 1