By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,983 Members | 1,468 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,983 IT Pros & Developers. It's quick & easy.

simple regular expression

P: n/a
What would the regular expression be to return both of the following?
Thank you.

'earns $21.25 from"
'earns $21 from"
Nov 21 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Is there anything better than: 'earns \$\d+\.*\d* from' ?

"George Yachán" <2m*******@wherever.com> wrote in message
news:Oq****************@TK2MSFTNGP11.phx.gbl...
What would the regular expression be to return both of the following?
Thank you.

'earns $21.25 from"
'earns $21 from"

Nov 21 '05 #2

P: n/a
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/de...geElements.asp

Hope this helps
Jay

"George Yachán" <2m*******@wherever.com> wrote in message
news:Ot****************@tk2msftngp13.phx.gbl...
Is there anything better than: 'earns \$\d+\.*\d* from' ?

"George Yachán" <2m*******@wherever.com> wrote in message
news:Oq****************@TK2MSFTNGP11.phx.gbl...
What would the regular expression be to return both of the following?
Thank you.

'earns $21.25 from"
'earns $21 from"


Nov 21 '05 #3

P: n/a
A response from heaven! That's everything I need to know and more. Thank you
very much!!

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:uM**************@TK2MSFTNGP15.phx.gbl...
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/de...geElements.asp
Hope this helps
Jay

"George Yachán" <2m*******@wherever.com> wrote in message
news:Ot****************@tk2msftngp13.phx.gbl...
Is there anything better than: 'earns \$\d+\.*\d* from' ?

"George Yachán" <2m*******@wherever.com> wrote in message
news:Oq****************@TK2MSFTNGP11.phx.gbl...
What would the regular expression be to return both of the following?
Thank you.

'earns $21.25 from"
'earns $21 from"



Nov 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.