Quantifiers in regular expressions are greedy : they try to match as much text as possible. Since your last " is optional (did you write "? In your regular expression) .+ Will match it.
Using [^"] is one acceptable solution. The disadvantage is that your string cannot contain the characters " (which may or may not be desirable, depending on the case).
Another is the requirement " :
regexp = '"(.+)"'
Another is to make + inanimate using +? . However, you also need to add the ^ and $ bindings (or similar, depending on the context), otherwise it will correspond only to the first character ( t in the case of "test" ):
regexp = '^"?(.+?)"?$'
This regular expression allows characters to " be in the middle of the string, so that "t"e"s"t" will cause the group t"e"s"t be captured by the group.
source share