0

I have a regex which will match amount in $.

 (\-?\$\-?[0-9]+\.?[0-9]*|usd\-?[0-9]+\.?[0-9]*|[0-9]+\.?[0-9]*\-?usd|[0-9]*\.?[0-9]*\$)

Currently its matching for $250, USD250 etc, it should not match the $250 in $250abchhh.

So, I tried word boundary, but it didn't fix the issue as well, how can I fix this issue?

matching cases are

 $456 
 $45.6
 $.5
 $-45
 -$45
 usd-456
 usd46
 usd4.6
 usd.46
 1$
 1.5$
 .5$
 -.5$
 5usd
 456usd

it should not match

  455$abc
  abc$123
  abcuds1
  jhb$5665usdjnjnb
  $usd1555
  usd$768
  $566usd
  $5788usdbjhj  
sree vidya
  • 33
  • 8
  • But your regex has no problems you are talking about. Look here:https://regex101.com/r/rAJLc0/6. You have problems when you have both $ and usd and in work with decimal dot. What exactly do you want to solve? "but it only matches it with a separate amount." - the phrase has no sense, sorry. – Gangnus Jul 28 '17 at 08:41
  • removed that sentence – sree vidya Jul 28 '17 at 09:13
  • It is different question than the supposed duplicate. That another one asks how to read dollar sums more simply. This one asks how to cut off the unwanted parts of line. – Gangnus Jul 28 '17 at 10:53

1 Answers1

0

You should also write a correct regex for a decimal. And use ^$ for start and end of line.

^\s*(?:(?:(?:-?(?:usd|\$)|(?:usd|\$)-)(?:(?:0|[1-9]\d*)?(?:\.\d+)?(?<=\d)))|(?:-?(?:(?:0|[1-9]\d*)?(?:\.\d+)?(?<=\d))(?:usd|\$)))\s*$

Look here at the test results.

Gangnus
  • 24,044
  • 16
  • 90
  • 149