how to make regex below to detect also prices like just £7 not only everything > 9
/\d[\d\,开发者_开发知识库\.]+/is
thanks
to match a single digit, you can change it to
/\d[\d,.]*/
the +
means require one or more, so that's why the whole thing won't match just a 7
. The *
is 0 or more, so an extra digit or ,
or .
becomes optional.
The longer answer might be more complicated. For example, in the book Regular Expression Cookbook, there is an excerpt: (remove the ^
and $
if you want it to match the 2
in apple $2 each
) but note that when the number is 1000 or more, the ,
is needed. For example, the first regex won't match 1000.33
(unsourced image from a book removed)
Your expression would allow 123...3456
... I think you might want something like (£|$|€)?\d\d+((,|.)\d{2})?
This will require the source have a currency symbol, and two digits for cents with a separator.
You might look at a regex more like the following.
/(?:\d+[,.]?\d*)|(?:[,.]\d+)/
Test Set:
5.00 $7.00 6123.58 $1 .75
Result Set:
[0] => 5.00 [1] => 7.00 [2] => 6123.58 [3] => 1 [4] => .75
EDIT: Additional Case added
精彩评论