我有以下字符串:
"您已选择:7年固定价格8.25美分/件">
我如何只提取";8.25〃;从C#中的这个字符串?
我尝试了以下regex选项:CCD_ 1和CCD_;7〃;,这是它找到的第一个数字。
对于这个特定的例子,您可以使用一个命名的捕获组,并尝试如下操作:
(?<float>d+.d+)
以下是一个快速演示:https://dotnetfiddle.net/Dm78d7
使用
d+(?:.d+)?(?=p{Sc}/item)
见证明。
解释
--------------------------------------------------------------------------------
d+ digits (0-9) (1 or more times (matching
the most amount possible))
--------------------------------------------------------------------------------
(?: group, but do not capture (optional
(matching the most amount possible)):
--------------------------------------------------------------------------------
. '.'
--------------------------------------------------------------------------------
d+ digits (0-9) (1 or more times (matching
the most amount possible))
--------------------------------------------------------------------------------
)? end of grouping
--------------------------------------------------------------------------------
(?= look ahead to see if there is:
--------------------------------------------------------------------------------
p{Sc} a currency sign
--------------------------------------------------------------------------------
/item '/item'
--------------------------------------------------------------------------------
) end of look-ahead