user2788650
user2788650

Reputation:

regex for number followed by letters

I am trying get the Regex right for the following scenario but have some trouble. Below is the scenario.

My string looks like this: 141st ST or 141ST ST

I want to get only the 141st part

Upvotes: 1

Views: 2591

Answers (2)

Mahmut Ali ÖZKURAN
Mahmut Ali ÖZKURAN

Reputation: 1140

This should work.

[0-9]{3}[A-Za-z]{2}

3 numbers with

[0-9]{3}

1 to 3 digits with

[0-9]{1,3}

and 2 letters with

[A-Za-z]{2}

Upvotes: 3

tenub
tenub

Reputation: 3446

If you want to be cool you could do it this way to validate the street number too:

\d{0,2}(?:1st|2nd|3rd|[04-9]th)(?= st)

and just use the case insensitive flag

Upvotes: 1

Related Questions