From the command line using GNU grep with libpcre:
$ echo '$4,233.65 $5,073.64 $9,307.29 $9,273.41 $0.00 $0.00 $33.88' \
|grep -Po '^(?:[^$]*\$){3}\K\S+'
9,307.29
(Explanation at Regex101) This uses a variable-width positive look-behind, which not all languages support, as simplified by \K
(foo\Kbar
is identical to (?<=foo)bar
, matching "bar" from "foobar"). This skips two dollar amounts (it uses {3}
because we're also including the lead $
since that's not part of the desired match) and then matches the next non-white-space characters.
You can use the same logic in Javascript:
let test = "$4,233.65 $5,073.64 $9,307.29 $9,273.41 $0.00 $0.00 $33.88";
test.match(/^(?:[^$]*\$){3}(\S+)/)[1]; // "9,307.29"
This is basically the same regex (explanation at Regex101), but instead of using \K
before the match, I've got the desired portion in the first capture group, which match()
saves in array index 1 (index 0 is the whole match, including the leading part since we're not using …\K
or (?<=…)
to make it zero-width).
However, if you're using a programming language like Javascript, you are better off doing it more programmatically:
let test = "$4,233.65 $5,073.64 $9,307.29 $9,273.41 $0.00 $0.00 $33.88";
test.match(/\$\S+/g)[2].substring(1); // "9,307.29"
(Explanation at Regex101) This is more non-regex code, but much much cleaner. Here, I'm merely looking for dollar values, grabbing the third one (recall that arrays are zero-indexed), and using substring()
to strip off the leading $
(strings are also zero-indexed).
Note, Javascript does not support look-behinds like …\K
or (?<=…)