Reputation: 23
I'm trying to figure out how to capture a regex and the two lines previous to it.
Example:
Santa Claus
North Pole, North Pole
H0H 0H0
The regex I have is for the Postal Code [a-z]{1}\d{1}[a-z]{1}\s\d{1}[a-z]{1}\d{1}
I want to be able to capture that result and the previous two lines as well using on regex expression.
Does anyone have any ideas?
Thank you in advance.
Upvotes: 2
Views: 54
Reputation: 240928
You could use the following:
(.*\n.*\n[a-z]\d[a-z]\s\d[a-z]\d)
.*\n.*\n
will match all characters on the previous two lines.[a-z]\d[a-z]\s\d[a-z]\d
- I removed {1}
after each character class (since only one will be matched by default, this is redundant).You may also need to add the case-insensitive i
flag since [a-z]
will only match lowercase characters. Otherwise that should be replaced with [A-Za-z]
to catch the capital letters in the postal codes.
Upvotes: 1