in reply to \q quote-matching operator

If you were trying to match quotes in text taken from a MS Office document, then that would explain the trouble you were having. By default, MS converts " and ' into "Smart Quotes".

A similar problem for \q is it would need to be locale dependant since different languages use different quoting schemes.

/\".+\"/

seems to work at first, and it will work for very simple cases, but it would probably not do what you wanted for the expresssion

ABC "DEF" GHI "JKL" MNO

where it would match "DEF" GHI "JKL". To match "DEF" and "JKL" seperately, you would need something like:

/\".+?\"/

We probably should use .*? instead of .+? to handle things like "". But, if you are dealing with code, none of these solutions handles things like "AB\"C". There is a common boilerplate regex for this, but at this point you may want to take a look at the documenation for Text::Balanced which is a core module for 5.8 (and probably earlier). If provides matching solutions for all sorts of common problems like this.

Ted

PS: It is quite possible you already new all of this and had left it out in the interest of brevity. :-)

Replies are listed 'Best First'.
Re^2: \q quote-matching operator
by hardburn (Abbot) on Aug 26, 2004 at 12:41 UTC

    Greediness is a well-known problem, and is hardly limited to quotes. That example is better written as:

    /" [^"]+ "/x;

    "There is no shame in being self-taught, only in not trying to learn in the first place." -- Atrus, Myst: The Book of D'ni.