2016-10-23 3 views

답변

1

(?i)가 문자를 구분 정규식 케이스를 만드는 :로

text <- c("Machine Learning is my fav topic.", "I love machinelearning.") 

ifelse((found <- regexpr("\\sMachine Learning", text, 
        perl =TRUE)) !=-1, substring(text, found, 
          found+attr(found,"match.length")), "nothing found") 

그러나 그것은 나를 반환 ..

"nothing found" "nothing found" 

나는 결과를 얻을해야합니다. 당신이 두 구문을 검색 할 때

1) : 패턴 '학습'

library(stringr) 
unlist(str_extract_all(text, "(?i)Machine\\s*Learning")) 
#[1] "Machine Learning" "machinelearning" 
1

나는 2 점을 가지고는 아래를 참조하시기 바랍니다 다음에 0 개 이상의 공간 (\\s*) 다음에 '기계'를 사용하여 말하지만, 당신은 "machine \ s? learning"이라는 표현을 사용해야합니다. ? \ s 뒤에는 공백이 무시됩니다.

2) regexpr을 사용하여 일치 항목을 찾은 다음 regmatches() 함수를 사용하여 텍스트를 추출하십시오.

> text <- c("Machine Learning is my fav topic.", "I love machinelearning.") 
> m <- regexpr("machine\\s?learning", text, perl=TRUE, ignore.case = TRUE) 
> regmatches (text, m) 

[1] "Machine Learning" "machinelearning" 
관련 문제