I understand that a question mark at the beginning of a capture group (?:pattern) indicates that this pattern should not create a backreference, but what does it mean in the following example where the question mark is at the end of the capture group after the wildcard?
self =~ /(.*?)_(\d+)$/
Code
class String
# used to instantiate a model based on a dom_id style
# identifier like "person_10"
def to_model
self =~ /(.*?)_(\d+)$/
class_name, id = $1, $2
class_name.classify.constantize.find(id)
end
end
It is non-greedy expansion. A trailing
?converts*and+from greedy to non-greedy. A non-greedy wildcard will select the smallest matching set of characters, not the largest possible. See this blog for an explanation