여기에 여러 개의 정규식 평가가 필요하지만 수행 할 작업 (텍스트 제외 모든 항목)을 가져 오는 출력이 하나 있습니다. 이 게시물을 보면 루비에서 복수 정규식 평가를 피하기 위해 gsub 대신 select 사용하기
words = IO.read("file.txt").
gsub(/\s/, ""). # delete white spaces
gsub(".",""). # delete periods
gsub(",",""). # delete commas
gsub("?","") # delete Q marks
puts words
# output
# WheninthecourseofhumaneventsitbecomesnecessaryIwanttobelieveyoureallyIdobutwhoamItoblameWhenthefactsarecountedthenumberswillbereportedLotsoflaughsCharlieIthinkIheardthatonetentimesbefore
-
Ruby gsub : is there a better way은 - 나는 여러 정규식 평가없이 동일한 결과를 달성하기 위해 일치를 수행하려고 할 것입니다 생각. 그러나 나는 같은 결과를 얻지 못한다.
words = IO.read("file.txt").
match(/(\w*)+/)
puts words
# output - this only gets the first word
# When
그리고 이것은 첫 번째 문장 가져옵니다 경기보다는 GSUB에 (공백와 비 단어 문자를 제거 포함) 동일한 출력을 얻기에
words = IO.read("file.txt").
match(/(...*)+/)
puts words
# output - this only gets the first sentence
# When in the course of human events it becomes necessary.
어떤 제안?
감사합니다. 나는 그것을 얻었다 고 생각한다. 이것은 공백 문자 (\ s), 마침표 (.) 또는 쉼표 (?)를 평가하고 아무 것도 사용하지 않습니다 (따옴표 사이에 아무 것도 없기 때문에). 도움이됩니다. 그냥 정규식의 교수형에 노력을 계속해야합니다. – drollwit