simple regular expression

  • Thread starter Thread starter George Yachán
  • Start date Start date
G

George Yachán

What would the regular expression be to return both of the following?
Thank you.

'earns $21.25 from"
'earns $21 from"
 
George,
Assuming you mean a number preceded by a dollar sign, I would use:

'earns \$\d+(\.\d*)? from'

As I would expect you only want a single period, and the period needs to
proceed the (optional) decimal places.

Something like:
Const pattern As String = "earns (?<amount>\$\d+(\.\d*)?) from"
'Const pattern As String = "earns (?<amount>\$\d+\.*\d*) from"
Dim theRegex As New System.Text.RegularExpressions.Regex(pattern)

Debug.WriteLine(theRegex.IsMatch("earns $21..25 from"), "$21..25")
Debug.WriteLine(theRegex.IsMatch("earns $21.25 from"), "$21.25")
Debug.WriteLine(theRegex.IsMatch("earns $21 from"), "$21")

If you use named groups as I have above, you can extra just the amount that
was matched:

Something like:

Dim theMatch As System.Text.RegularExpressions.Match =
theRegex.Match("earns $21.25 from")
Debug.WriteLine(theMatch.Groups("amount"), "amount")



The following site provides a good overview of regular expressions:

http://www.regular-expressions.info/

While this site provides the syntax specifically supported by .NET:

http://msdn.microsoft.com/library/d...l/cpconRegularExpressionsLanguageElements.asp

Hope this helps
Jay
 
A response from heaven! That's everything I need to know and more. Thank you
very much!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top