c - 使用正则表达式标识一个特定字段



对基本问题表示歉意,但我是正则表达式的新手,并且真的很难找到解决我所面临的问题的方法。

我正在尝试动态地从 json 响应中提取特定字段,每次调用它时都会更改。

响应是:

[{"colorPartNumber":"10045112022164298","skuPartNumber":"0400218072057","productColor":{"identifier":"Dark blue","label":"Dark blue","hex":"#0000A0"},"productSize":{"identifier":"0","label":"0","name":"Designer","scaleLabel":"apparel-wmn","schema":{"name":"UK","labels":["8"]}},"soldOut":true,"onlyOneLeft":false,"limitedAvailability":false,"preorder":false,"comingSoon":false,"visible":true,"displayable":true,"buyable":false,"availableInPhysicalStore":false,"expectedShippingDate":null},{"colorPartNumber":"10045112022164298","skuPartNumber":"0400094632819","productColor":{"identifier":"Dark blue","label":"Dark blue","hex":"#0000A0"},"productSize":{"identifier":"1","label":"1","name":"Designer","scaleLabel":"apparel-wmn","schema":{"name":"UK","labels":["10"]}},"soldOut":true,"onlyOneLeft":false,"limitedAvailability":false,"preorder":false,"comingSoon":false,"visible":true,"displayable":true,"buyable":false,"availableInPhysicalStore":false,"expectedShippingDate":null},{"colorPartNumber":"10045112022164298","skuPartNumber":"0400218072040","productColor":{"identifier":"Dark blue","label":"Dark blue","hex":"#0000A0"},"productSize":{"identifier":"2","label":"2","name":"Designer","scaleLabel":"apparel-wmn","schema":{"name":"UK","labels":["12"]}},"soldOut":true,"onlyOneLeft":false,"limitedAvailability":false,"preorder":false,"comingSoon":false,"visible":true,"displayable":true,"buyable":false,"availableInPhysicalStore":false,"expectedShippingDate":null},{"colorPartNumber":"10045112022164298","skuPartNumber":"0400468014814","productColor":{"identifier":"Dark blue","label":"Dark blue","hex":"#0000A0"},"productSize":{"identifier":"3","label":"3","name":"Designer","scaleLabel":"apparel-wmn","schema":{"name":"UK","labels":["14"]}},"soldOut":false,"onlyOneLeft":true,"limitedAvailability":false,"preorder":false,"comingSoon":false,"visible":true,"displayable":true,"buyable":true,"availableInPhysicalStore":false,"expectedShippingDate":null}]

我正在尝试提取 skuPartNumber,但仅当"可购买"值设置为 true 时。

我尝试的每一件事似乎都无法只得到这一个价值:(

所以在上面的例子中,我唯一想拉出的值是0400468014814.

此 json 是动态的,因此可能会返回 100 个值,但原理是相同的。

失败尝试的一个例子是skuPartNumber(.*?)"buyable":true,它只给了我第一个值 (0400218072057),这是错误的。

再次对基本问题感到抱歉。

尝试以下正则表达式:

"skuPartNumber":"(d+)"(?:[^}]*?}){3}[^}]*?"buyable":true

您的答案在第一个匹配组中。

最新更新