Steven
Steven

Reputation: 18869

C# Regex help - retrieving a number from a sentence

So I have some text that I need to get an integer out of. Here's some examples of what the text could look like:

as low as $99

you can get it for $99 or less

I need to get the number out of the text, either with or without the $ sign.

Can I do this using Regex.Replace?

Upvotes: 0

Views: 125

Answers (2)

TheCodeKing
TheCodeKing

Reputation: 19220

If you specifically want money values prefixed by $ you could use a look behind:

(?<=\$)[0-9\.]+

Upvotes: 1

davisoa
davisoa

Reputation: 5439

I would use RegEx.Match. You can use the following regex: ([0-9]+)

This is assuming you don't have any commas, or decimals that may mess it up. If that is an issue, just add what else to look for inside the [].

The Match function will return an object that will tell you if there were any matches along with an array of the matches, and so you can easily get everything that was found.

Upvotes: 5

Related Questions