0

I'm terrible at regex so forgive the basicness of this question. I currently have the following regex that will identify a UK postcode at the end of a string:

([a-zA-Z]{1,2}([0-9]{1,2}|[0-9][a-zA-Z])\s*[0-9][a-zA-Z]{2}$)

And this works fine. However I'm now trying to do the opposite in that I want a regex that will identify everything else in the string, but NOT the postcode at the end of it. I've amended the regex as so:

((.+)[a-zA-Z]{1,2}([0-9]{1,2}|[0-9][a-zA-Z])\s*[0-9][a-zA-Z]{2}$)

This successfully breaks it into capture groups, the string minus the postcode is identified (using a regex tester) as capture group 1. However, I don't know how to get it to select capture group 1 only?

Alireza
  • 100,211
  • 27
  • 269
  • 172

1 Answers1

0

You can use a positive lookahead (?=) to select the string without the postcode.
Applied to your regex this will result in the following:

((.+)(?=[a-zA-Z]{1,2}([0-9]{1,2}|[0-9][a-zA-Z])\s*[0-9][a-zA-Z]{2}$))

the positive lookahead will find an expression where another expression follows, so A(?=B) will find expression A where it is followed by expression B

more about lookarounds on stackoverflow

ScintillatingSpider
  • 338
  • 1
  • 9
  • 14