I can't find a word in line with the line started special word. For example;
Mike likes banana, kiwi and grapes.
Mary likes banana, apple and watermelon.
I made a mistake in Mary's favorite fruit. I want to change banana to orange. But how can find banana only starting with Mary line. I need ruby regex because of I am using it in chef cookbook.
Before;
Mike likes banana, kiwi and grapes.
Mary likes banana, apple and watermelon.
After;
Mike likes banana, kiwi and grapes.
Mary likes orange, apple and watermelon.
Best Regards
Hasan
We may construct the method
I will display several examples for which the first line of the string (
str) and last three arguments are fixed, namelyIt is convenient to write
The only thing that changes in the examples is the second line of the string, (containing
"Mary").The examples considered are as follows.
In all examples the regular expression used by String#sub evaluates to the following.
The elements of the expression are as follows.
Note that the positive lookahead asserts that the string
"Mary"is somewhere in the line.