1

I want to capture only 136.50 in the following

   $136.50 (was $195.00)

I'm trying to use a negative lookbehind in order not to group a price if it is preceded by another price. This is what I have:

(?<!\$.+)\$(?<price>[\d.,]+)

what am I doing wrong?

EDIT: I think the problem might be that I can't use a variable length string inside a lookaround, but I'm not sure how to construct the regex without one.

EDIT: There will sometimes be only one price, in which case I want to group that price. This is why I did not use a positive lookahead to confirm a price is the first price.

4

3 回答 3

3

不要使用否定的后视,只取第一个价格(在第一组中,如果有分数,可能在第二组中)。可能是这样的?

/^.*?\$([\d,]+)(\.\d\d)?/
于 2012-10-26T18:30:19.853 回答
0

好的,所以我认为您也应该采取第一个价格:

这应该匹配所有价格,包括。成千上万的 koma,就像来自 owlstead 的这个 /^.*?\$(\d{1,3}(?:,\d{3})*(?:\.\d+)?).*$/

于 2012-10-26T18:37:46.400 回答
0

你可以使用这样的东西

/(\$\d*.\d*)(?!.\1)/

这将匹配第一组,而不是同一组

于 2012-10-26T18:37:56.463 回答