R regex匹配模式并替换为动态值
在R中,我需要想出一种从以下数据中提取的方法:R regex匹配模式并替换为动态值,regex,r,replace,match,Regex,R,Replace,Match,在R中,我需要想出一种从以下数据中提取的方法: Using Notifications API: Strategy ON_TARGET:LookForward Ratio: 0.25 Num of hours back: 6 Rpr Cfg--> FP: 9.02 CP: 18.04 MSR: 0.51 MPR: 0.49 TTRR: 0.51. RepricingMethod_ReachTopRankOnTarget: Sampling info: Dilutions: 2_1_1_7
Using Notifications API: Strategy ON_TARGET:LookForward Ratio: 0.25 Num of hours back: 6 Rpr Cfg--> FP: 9.02 CP: 18.04 MSR: 0.51 MPR: 0.49 TTRR: 0.51. RepricingMethod_ReachTopRankOnTarget: Sampling info: Dilutions: 2_1_1_7_4_2-AKQSGIBQINE7B, 1_1_1_3_4_2-A2TBTG410J2ORU, 1_1_1_5_4_2-AQNACJEM8PUJ1, 1_1_1_6_4_2-A2R0FX412W1BDT, 1_1_1_6_4_2-A1TFBK3C7LO58P, 1_1_1_4_4_2-A1WMYU489BEYM4, 1_1_1_7_3_2-A3G2RBEZBLAJ53, 1_1_1_5_4_2-A2RFSIF56F6W5J, 1_1_1_6_4_2-AAL7E530K3IX2, 1_1_1_7_4_2-AFAGM2K2OIRAD, 1_1_1_6_4_2-APRB74G8QOR1X, 1_1_1_5_3_2-A24POLY6RKLSW7, 1_1_1_5_4_2-A18IJX3G7FD5MC, 1_1_1_5_3_2-A1UEW3GW612BDQ, 1_1_1_7_4_2-A2G88111572J8M, 1_1_1_4_4_2-A2IKHPELK48TV7, 1_1_1_7_4_2-A79CLRHOQ3NF4, Additions: None.New TTR: 0.9 Merchant is not top ranked, merchant group known (2_1_1_7_4_2-AKQSGIBQINE7B), top ranked group known (2_1_1_7_4_2-ATVPDKIKX0DER), taking prices of 1 groups above/equal merchant group as reference. Lowest landing price within these groups is 9.19 . Initially suggested change: 0.0, change determined upon historical analysis: 0.0 with significance of 1.0, prediction for top rank: false, state trace: -_S6_S8- HWP: null- LLP: 9.02 Reducing price obtained by history to 8.91. Modification: New price lower than floor price, reset to floor price. Target optimization: Current top rank ratio is 0.0, different optimal ratio was not found.
出现在模式“TTRR:”之后并以点(.)结尾的数值。在上述示例中,返回的值应为0.51。
有什么想法吗?你可以试试下面的正则表达式
"TTRR:\\s*\\K\\d+(?:\\.\\d+)?(?=\\.)"
将迄今为止匹配的文本保留在整个正则表达式匹配之外
您可以在这里使用基本替换
sub('.*TTRR:\\s*([0-9.]+)\\..*', '\\1', x)
# [1] "0.51"
效果很好。谢谢你的快速回复。因为它是一个相当长的表达式,你能解释一下每个部分的作用吗?或者你有一个很好的文档来源?
> gsub("TTRR:\\s*(\\d+(?:\\.\\d+))|.", "\\1", x)
[1] "0.51"
> gsub("TTRR:\\s*(\\d+(?:\\.\\d+))\\.|.", "\\1", x)
[1] "0.51"
sub('.*TTRR:\\s*([0-9.]+)\\..*', '\\1', x)
# [1] "0.51"