Home > other >  Capture values >= 20 $ (without cents, currency symbol and spaces)
Capture values >= 20 $ (without cents, currency symbol and spaces)

Time:01-23

I would like to capture $- (or £-)prices >= 20, without cents (pence), where the $ (£) may be in front of or after the value and the currency-symbol may be separated from the value by space(s) or not, e.g.:

$20
$3000
£ 60.67 (but only the '60'-part)
33$
500.99$ (but only the '500'-part)
90   £

Something like:

(?:[^\d][$£] ?)([\d]{3,}|[2-9][\d]{1})|([\d]{3,}|[2-9][\d]{1})(?: *.?[0-9]* ?[$£])

...which works, but simpler (or at least without the non-capturing (?: )-syntax because it doesn't work with my regex browser highlight extension.

I would like to use this to highlight prices e.g. on Amazon via a regex browser extension. If you happen to know a good one (which possibly even supports (?: )-syntax) I'd be happy to hear your suggestions, too :-)

Many thanks in advance

CodePudding user response:

You can use the following regex:

(?<=[$£])\s*([2-9]\d|\d{3,})(?=[\.\s])|(?<!\.)([2-9]\d|\d{3,})(?=(?:\.\d )?\s*[$£])

which splits the matching in two cases:

  • case 1: your money symbol is found before the numeric value
  • case 2: your money symbol is found after the numeric value

"Case 1": (?<=[$£])\s*([2-9]\d|\d{3,})(?=[\.\s])

  • (?<=[$£]): the money symbol is followed by...
  • \s*: spaces
  • ([2-9]\d|\d{3,}): 20 or bigger numeric values
  • (?=[\.\s]): followed by either a dot (if decimal) or a space

"Case 2": (?<!\.)([2-9]\d|\d{3,})(?=(?:\.\d )?\s*[$£])

  • (?<!\.): match is not preceeded by a dot
  • ([2-9]\d|\d{3,}): 20 or bigger numeric values
  • (?=(?:\.\d )?\s*[$£]): followed by optional dot and numbers (for decimal), mandatory spaces and the money symbol

Check the demo here.

  • Related