sadfaceman has asked for the wisdom of the Perl Monks concerning the following question:

Hello everyone, I am new to perl, and am working with some regular expressions, and im trying to figure out how to capture text between single quotes - but with a few caveats:
1. I need to ignore escaped quotes ie: \'
2. anything between quotes needs to be longer than 3 characters
3. needs to be a global match , so I can catch multiple instances
I have tried:
/(?<!\\)\'(^\'{3,}?)(?<!\\)\'/
but a problem arises when I have multiple quoted statements on the same line, that are under 3 characters the line:
-Attribute '%s' of Xml Tag '<%s>' NOT present
should match "<%s>", but instead matches " of Xml Tag" is there anyway around this?

Replies are listed 'Best First'.
Re: quote regular expression
by pc88mxer (Vicar) on Jun 02, 2008 at 17:28 UTC
    I think you should handle the greater than three characters constraint outside of the regular expression. Also, can you assume that an escaped single quote will only occur within a single quoted string? If so, the regex becomes very straight-forward:
    while ($text =~ m/'(((\\')|[^'])*)'/g) { if (length($1) > 3) { ...do something... } }
    The thing about the short string constraint is that you still want the regex engine to match and move past them even though you're not interested in processing them. That suggests you should perform the length check outside of the regex.

    Also, do you have to handle \\? If so, just modify the regex as follows:

    while ($text =~ m/'(((\\['\\])|[^'])*)'/g) {
      that worked! thank you so much
Re: quote regular expression
by kyle (Abbot) on Jun 02, 2008 at 18:43 UTC