Hi to everybody,
Often I need to read a CSV file with delimited by commas. In order to find more quickly the value of a variable, inside the lines I use the matching regular expression $variable =~ m/.../.
Today I read a file with the 'C++' string inside (Only later I have understood that responsibility was of the 'C++' string).
The script has stopped with the following error message:

Nested quantifiers in regex; marked by <-- HERE in m/C++ <-- HERE / at....

So I have created a dedicated test script in order to verify the problem:
use strict; #looked variable my $VarString = 'C++'; my $Counter = 0; #example array (it simulates the file lines) my @Array; #push variable (CSV delimited) in array $Array[0] = 'VISUAL BASIC,PERL,PASCAL'; $Array[1] = 'C#,C,C++'; $Array[2] = 'ASSEMBLER,PHP,JAVA'; $Array[3] = 'HTML,XML,JAVA'; print "Start...\n"; #search string in array while ($Counter <= $#Array){ #check with the matching expression next unless $Array[$Counter] =~ m/$VarString/; print "I have found the string $VarString in array position $Count +er!\n"; }continue{ #increment the array position $Counter++; } print "Stop\n";
In this code if the $VarString (string looked for) is 'PHP' (for example) it's all okay, but if you set 'C++' the problem arrives. Because the characters '++' they are considered inside of the match pattern.

Thanks for all the suggestions! M.

In reply to Regular Expression problem with $variable =~ m/C++/ by Manlio

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.