1

How do I match two numbers, each preceded by dollar signs and with a hyphen between them, such as '$1,000 - $2,000'. I'd like to get the full range with the dollar signs included.

'$1,000 - $2,000'

Other examples:

'$30 - $40'

'$1-$2'


I've tried: ([\$0-9, -]*){1} It doesn't work when there are characters before.

goldisfine
  • 4,742
  • 11
  • 59
  • 83
  • 4
    You can match `$1,000 - $2,000` in the string `$1,000 - $2,000` using the regular expression `.*`. To get a good answer to a regular expression question, you need to be a lot clearer about 1) what kinds of variable things you want to match, 2) what kinds of things look similar that you *don't* want to match, and 3) what the input looks like that contains the things from 1) and 2) – Gareth Aug 10 '15 at 23:28
  • For the actual pattern '$1,000 - $2,000' or for anything in that range with a dollar sign and a comma for thousands? – isosceleswheel Aug 10 '15 at 23:39
  • what about decimals `.`, two digits or no `.`? what about leading zeros? Is the `,` optional or do you have to build groups of 3 digits? Is the amount of spaces between the hypen optional? Is the `$` mandatory? Is there an upper bound? Do you also accept negative amounts? – maraca Aug 10 '15 at 23:53
  • [I've answered a similar question, it wouldn't be hard to make it work for your case](http://stackoverflow.com/questions/16242449/regex-currency-validation) – Gary Aug 11 '15 at 00:39

2 Answers2

1
\$1[0-9][0-9][1-9]\s

That seems simple to me...if you are excluding 1000 and 2000...if this is including 1000 and 2000 then

\$((1[0-9][0-9][0-9])|(2000))\s

There is surely a cleaner way to do it...but this will do the job

**********************edit*************************

Now that you changed your question... I think a simple regex would be

\$[0-9]*\s?\-\s?\$[0-9]*\s?

Dollar sign followed by any amount of numbers,

optional white space,

dash,

optional white space,

dollar sign followed by any numbers,

optional white space.

Seanoseanohay
  • 341
  • 4
  • 19
  • 1
    you could remove the braces around 2000 and the other numbers and you could write [0-9]{3} but whether this is cleaner or not is probably a matter of taste. – maraca Aug 10 '15 at 23:41
0

To match '$1,000 - $2,000', you just use '$1,000 - $2,000' as the regular expression. To match anything in the range 1000 - 2000 with the format $x,xxx, use the regex:

'\$1,\d{3}|\$2,000'

To match any range between with 1000 as lower bound and 2000 as upper bound with the format $x,xxx - $x,xxx, use the regex:

'\$1,\d{3}|\$2,000 - \$1,\d{3}|\$2,000'
isosceleswheel
  • 1,516
  • 12
  • 20