tags:

views:

213

answers:

3

I.e. the match of

 He said, "For my part, it was Greek to me. "

should be

For my part, it was Greek to me.

Without the trailing space. I've tried

\"\s*([^\"]*)\s*\"

but it's always matching trailing whitespaces in the group.

+1  A: 

Wouldn't it be easier to just trim the blanks afterwards using simple string functions? This would also simplify the regular expression.

Sebastian Dietz
While many people over-code with regular expressions, the flipside of the argument is that regular expressions do exactly one thing, which is robust string pattern-matching. It's a waste to keep a couple characters out of a regex for simplicity, to add another cleanup step.
I would argue that the string functions are more efficient than a more complicated regex. And it makes the code more readable when you just trim the blanks by conventional means.
Sebastian Dietz
Agree. This is the first thing I thought of. Of course, there's nothing wrong with the fully functional regex except for the arguable readability issue.
Instantsoup
+2  A: 

try:

\"\s*([^\"]*?)\s*\"

But be careful, this version is contingent upon the closing quotation mark (because it's lazy). When you add the lazy operator here, it grabs the smallest possible string it can. If there are no closing quotes, this regular expression will fail

If you're sure there's no empty strings, you can use:

\"\s*([^\"]*[^\"\s])\s*\"

The main problem with this version is that you are now pushing for a minimum of one character.

Honestly, looking at it twice, I'd use the first pattern. You're looking for the closing quote and not escaping any quotes. It'll work 100% of the time for your current needs.

This could do with a better explanation. I also think your reservations about empty strings is wrong - the first regex should still pull an empty string out of " " or "". However, I think this is the correct answer.
SpoonMeiser
A: 

What about if you force it to have the final character be non-white space? Something like this:

\"\s*([^\"]*[^\s])\s*\"
Ben