2016-06-15 2 views
-1

에서 와일드 카드 :사용 나는 같은 것을 할 싶습니다 GSUB

"String with text and replaceText and replaceText and so on... " 

결과

"String with text and $abc$ and $def$ and so on... ".gsub("$*$", "replaceText") 

하지만 난 아무 생각이 어떻게 여기 * 일 같은 와일드 카드.

+0

그것은 와일드 카드가 아닙니다.. 그것은 그렇게 작동하지 않습니다. – sawa

답변

4

당신은 당신이 탈출해야합니다

"String with text and $abc$ and $def$ and so on... ".gsub(/\$\w+\$/, "replaceText") 
#=> "String with text and replaceText and replaceText and so on... " 

는 기억이 목적을 위해 정규식을 사용할 수 있습니다 $

1

호기심에서 벗어나 : 더 많은 구획 문자가 있는지 여부 (do not do 그녀는 back-references을 사용할 수 있습니다) 만 서명 llar : 내부 []$ (*도 탈출 할 필요가 없습니다

str = "String with text and $abc$ and *def* and so on... " 
str.gsub(/(?<delim>[$*]).*?\k<delim>/, "replaceText") 
#⇒ "String with text and replaceText and replaceText and so on... " 

주)

+0

"ä"또는 "ü"와 같은 특수 문자로도 작동합니다. – kernification

+0

발음 구별 부호가 포함 된 일부 유럽 알파벳의 문자와 함께 사용되는 방법을 이해했는지 확신 할 수 없습니다. – mudasobwa

관련 문제