Hello I only know basic regex and need some help figuring this out:
I’m extracting the addresses in a txt file where the line starts with:
Service Address: ACB st, City, Province, Postal code
the issue is that some of these lines have this after the address itself: Page 1 / 1 (and this can be Page 2/2 or Page 3/3 etc. but not all address lines have these.)
I need the regex to extract the addresses only so the match should stop before “Page”. This expression below matches everything including the page part, can someone please show me how to modify it to exclude the page part?
Service Location:(.+)
To answer your question - your pattern “Service Location:(.+)” is greedy and will “grab” everything on the line including the words “Service Location:” and “Page 1”.
My Regex Pattern: (?<=Service Address:).*(?= Page \d)
My pattern will look for an anchor and then start AFTER the words “Service Address:” and will grab everything on the same line until it reaches the word “Page 1” or “Page 2”.
I must’ve have missed something in my first posting, tried both expressions but they don’t generate the matches I expected. here is some sample relevant lines:
Service Location: 36 BLUE JAYS WAY (SUITES), TORONTO Page 1 / 2
Service Location: 3600 HIGHWAY 7 WOODBRIDGE ON L4H 0A0
Service Location: 2118 BLOOR ST W (BULK), TORONTO Page 1 / 1
your help is highly appreciated!!
In line 2, it looks like there is no “Page” which means we need to update our pattern.
Check out this pattern. It doesn’t need the “Page” but will stop if it finds it.
Regex Pattern:
(?<=Service Location:).*(?= Page \d)|(?<=Service Location:).*(?=[\r\n])
You people are incredibly awesome! I’ve learnt so much from you!! I would mark all of your answers as solution if I could! and sorry I had “address” instead of “location” in my first posting, which screwed up the expression…