Home > OS >  regex to extract housenumber plus addition
regex to extract housenumber plus addition

Time:05-05

I'm looking for a regex that matches housenumbers combined with additions for all addresses below:

Breestraat 4
Breestraat 45
Breestraat 456
Dubbele Straat  4a
Dubbele Straat 4-a
5 meistraat 1a
5meistraat 12
5meistraat 12a
Teststraat 22-III

Now the following regex works, except in the first case. This is because the single digit housenummber is missed because of the first \d in the regex (which prevents a starting digit to be captured).

\d?.(\d . )$

regex to extract housenumber addition

I'm scratching my head how to get the housenumer '4' for the first line. so basically how to change the "skip starting digit" to "skip starting digit but let it have to result on the capturing group".

CodePudding user response:

You can use

\d \D*$
\d \S*$

See the regex demo #1 and regex demo #2.

The pattern matches

  • \d - one or more digits
  • \D* - zero or more non-digit chars
  • \S* - zero or more non-whitespace chars
  • $ - end of string.

CodePudding user response:

It's not perfectly clear what you are requesting precisely..

Anyway this is the pattern matching the house number at the end of the string:

\d [-\da-zI]*$

https://regexr.com/6l0g7

Anyway I'm aware this is not a valid answer

  • Related