I have strings that come from outside to my application and may look like these with quotes:
Prefix content. "Some content goes here". More contents without quotes.
Prefix content. "Another "Additional" goes here". More contents without quotes.
Prefix content. "Just another "content". More contents without quotes.
The key note is that the strings come with quotes and I need to process these quotes properly. Actually I need to catch all the content inside quotes. I tried patterns like .*(".*").* and .*(".+").* but they seem to catch only content between two closest quotes.
Looks like you just want everything from the first quote to the last quote, even if there are other quotes between. This should suffice:
The leading and trailing
.*in your regex were never needed, and the leading one was distorting your results. It would initially consume the whole input, then back off just far enough to let the rest of the regex match, meaning the(".*")would only ever match the last two quotes.You don’t need the parentheses, either. The part of the string you’re after is now the entire match, so you can retrieve it with
group(0)instead ofgroup(1). If there may be newlines in the string and you want to match those, too, you can change it to:The
.metacharacter normally doesn’t match newlines, but(?s)turns onDOTALLmode for the rest of the regex.EDIT: I forgot to mention that you should be using the
search()method in this case, notmatch().match()only works if the match is found at the very beginning of the input, as if you had added the start anchor (e.g.,^".*").search()performs a more traditional regex match, where the match can appear anywhere in the input. (ref)