R正则表达式匹配模式并替换为动态值
R regex match pattern and replace with dynamic value
在 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_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+)?(?=\.)"
\K 将到目前为止匹配的文本保留在整个正则表达式匹配之外。
> x <- "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."
> m <- gregexpr("TTRR:\s*\K\d+(?:\.\d+)?(?=\.)", x, perl=TRUE)
> regmatches(x, m)
[[1]]
[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"
在 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_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+)?(?=\.)"
\K 将到目前为止匹配的文本保留在整个正则表达式匹配之外。
> x <- "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."
> m <- gregexpr("TTRR:\s*\K\d+(?:\.\d+)?(?=\.)", x, perl=TRUE)
> regmatches(x, m)
[[1]]
[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"