shmnff
shmnff

Reputation: 739

Regex: find only numbers in a string

I have a string

K9908098F, G2342D34324/ 234234323, 234-234-234; R324234

How to catch only 234234323 and 234-234-234 in VBA?

This [\d-]+ pattern grabs extra pieces

Upvotes: 1

Views: 1217

Answers (4)

Rahul Desai
Rahul Desai

Reputation: 15501

You are pretty close, just need to add borders: \b[\d-]+\b

Regex demo and explanation

Upvotes: 1

Thm Lee
Thm Lee

Reputation: 1236

You can try this too,

[-\d]+(?=[;,. ])

Demo

Upvotes: 0

Reck
Reck

Reputation: 1436

Not too elegant though but would work. Just a small addition to your regex.

[\d-]+[,;]

Upvotes: 0

builder-7000
builder-7000

Reputation: 7627

Give this a try:

(\w+),\s+([\w-]+);

This will capture 234234323 in group 1 and 234-234-234 in group 2.

Upvotes: 0

Related Questions