My Mother that title has been good to me:)
This is a mental note and maybe if you do not know that I speak not for you and you know that I speak already know.
I try to replace a part of a String by another party who is in the same String .
I mean, I have this “I like the heavy, and I do not like the country” ‘to’ and I want to get this one “I like the country and I do not like the heavy “.
It can be done well:
>> "I like the heavy, and I like the country not ". gsub ( / I like (< And >.*) I do not like the (.*)/, 'I like \ 2 and I do not like the \ 1 ' )
=> "I like country and I do not like the heavy "
Where
\ 1
and \ 2 are occurrences of the (.*).
The example may seem a bit silly, but this has been helpful to me very well to stay with the contents of a tag
html
:
" Content ". gsub ( /.* body [^>]*>(.*) \ / body < >>.*/ mix, ' \ 1 ' ). strip
=> "Content "
What can be solved in many other ways but I have found the easiest.
I write this note mentally for what little that is intuitive to use
‘\ 1’ string as a substitute because it is assumed that the strings in single quotes are not interpreted …
Another thing is that I always had to google solutions like this:
>> "I like the heavy, and I like the country not ". gsub ( / I like (< And >.*) I do not like the (.*)/, "I like # ($ 2) and not I like the # $ (1) " )
=> "I like country and I do not like the heavy "
And although it seems that no works is because the contents of
$ 1
and $ 2 has caught the gsub earlier rather than this:
"I like the music and I do not like flamenco ". gsub ( / I like (< And >.*) I do not like the (.*)/, "I like # ($ 2) and not I like the # $ (1) " )
=> "I like country and I do not like the heavy"
>> "I like the music and I do not like flamenco ". gsub ( / I like (.*< >) And I do not like the (.*)/, "I like # ($ 2) and I do not like the $ 1 (# ) " )
=> "I like flamenco and I do not like the music "