당신이 원하는 때 일치하는 구분 기호와 스테판의 의견처럼, 당신은 캡처와 split
를 사용해야 사이에있는 문자열 모두.
"This is a simple, uncomplicated sentence."
.split(/([,.])/)
# => ["This is a simple", ",", " uncomplicated sentence", "."]
서로 다른 배열로 구분하려면
는, 다음을 수행하십시오
a, x =
"This is a simple, uncomplicated sentence."
.split(/([,.])/).each_slice(2).to_a.transpose
a # => ["This is a simple", " uncomplicated sentence"]
x # => [",", "."]
또는
a =
"This is a simple, uncomplicated sentence."
.split(/([,.])/)
a.select.with_index{|_, i| i.even?}
# => ["This is a simple", " uncomplicated sentence"]
a.select.with_index{|_, i| i.odd?}
# => [",", "."]
귀하의 정규 표현식은 그런 식으로 (이스케이프 점) 작동하지 않습니다. – Stefan
코드 주석에서 x를 언급합니다. 여기서 X는 무엇입니까? – tomsoft
x는 내가 계산하고 싶은 것입니다. - 나는 a와 x를 둘 다 얻는 방법이 필요합니다. – kfmfe04