stringr :: string_extract [duplicate]를 사용하여 정규식에서 $를 이스케이프 할 수 없습니다.
Nov 15 2020
문자열에서 나는 다음 0.4%
에 오는 것을 추출하려고합니다.12 month(s)\n$500
검색 12 month(s)\n$500
후 뒤에 오는 숫자 점 번호를 가져옵니다.
에서 여기에 , "리터럴을 일치 시키려면"$” or “^”, you need to escape them, $, 및 ^. "하지만 이렇게하면 오류가 발생합니다.
Error: '\$' is an unrecognized escape in character string starting ""(?<=12 month(s)\\n\$"
내가 도대체 뭘 잘못하고있는 겁니까?
x <- "Term Minimum investment Rate Interest type Get this GIC\n3 month(s)\n$500 0.15% Simple\nChoose this GIC\n6 month(s)\n$500 0.25% Simple\nChoose this GIC\n9 month(s)\n$500 0.30% Simple\nChoose this GIC\n12 month(s)\n$500 0.40% Simple\nChoose this GIC\n18 month(s)\n$500 0.50% Simple\nChoose this GIC\n18 month(s)\n$500 0.50% Compound\nChoose this GIC"
as.numeric(stringr::str_extract(x, "(?<=12 month(s)\\n\$500)\\d\\.\\d{1,}"))
답변
RonakShah Nov 15 2020 at 07:31
R에서 이중 백 슬래시로 이스케이프해야합니다 \\
. ( ). 또한 열고 닫는 둥근 괄호 ( ()
) 를 이스케이프해야합니다 .
as.numeric(stringr::str_extract(x,"(?<=12 month\\(s\\)\n\\$500\\s)\\d\\.\\d{1,}"))
#[1] 0.4
기본 R에서 sub
:
as.numeric(sub('.*12 month\\(s\\)\n\\$500\\s(\\d\\.\\d{1,}).*', '\\1', x))