Bryon
Bryon

Reputation: 1

How do i get the rest of the digits?

Campus: Fairlane North
Building: testtesttest 5850 Mercury Dr
Floor: Floor 01 Common Area

Given the above test I want to extract "5850 Mercury"

I have tried /Building:.*(\d+\s\w+)/

Which gets me 0 Mercury

How do i get the rest of the digits? The number of digits can range from 1 to 6.

Thanks for the help.

Upvotes: 0

Views: 67

Answers (3)

Miller
Miller

Reputation: 35208

Three methods.

  1. Use non-greedy matching .*? instead of greedy matching .*

    /Building:.*?(\d+\s\w+)/
    
  2. Impose a word boundary \b to ensure you match the full number:

    /Building:.*(\b\d+\s\w+)/
    
  3. Change from the any character . to non-digits \D:

    /Building:\D*(\d+\s\w+)/
    

Upvotes: 5

user3077033
user3077033

Reputation: 167

You can do the following.

\b(\d{4} \w+)

Upvotes: -1

Dalorzo
Dalorzo

Reputation: 20014

How about this regex:

\b\d+\s\w+

Online Demo

Upvotes: 0

Related Questions