I want to delete the parts of a list of strings up to the first instance of an "=". But sometimes there is another "=" later in the strings. Since regex is generally "greedy," my code deletes too much. For example:
Is there a straight-forward way to convince sub() to stop at the shortest string that satisfies the match rather than the longest?
Thanks in advance for any help with this.
Larry Hunsicker
OK. I got it figured out. I was confused because the carat (^) is used in two different ways. At the beginning of the expression, it means the start of a line or string. But within brackets ([^ ]) it means any characters NOT including what follows the carat. So gsub("^[^=]*=", "", teststr) means: starting at the beginning of the string, match any number of characters _not including "=", but ending with an "=" and then replace that string with a null string (""). Turns out to be completely logical -- if initially confusing because of the two uses of the carat.
Larry Hunsicker
Hey @lhunsicker, glad you got it worked out. Another way of handling this with regular expressions would be to have made the pattern non-greedy by adding a ? to it:
If your inputs are always this regular, another good way to handle this would've been by splitting the strings by delimiter but the regex approach is pretty tidy.