in reply to strip text from a string

Hello KalaMonkey, and welcome to the Monastery!

Here is one way:

0:55 >perl -wE "my $s= qq[Can you please remove 'me' as that me is no + longer required, but this 'X' should go as well]; $s =~ s/(\s*'[^']* +?')//g; say qq[\n$s];" Can you please remove as that me is no longer required, but this shoul +d go as well 0:55 >

The character class [^'] matches any character other than an apostrophe, and the question mark added to the quantifier *? makes the match non-greedy.

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Replies are listed 'Best First'.
Re^2: strip text from a string
by johngg (Canon) on Sep 11, 2014 at 15:27 UTC
    s/(\s*'[^']*?')//g;

    I'm not sure why you enclose the pattern in parentheses as I don't think a capture is required. Also, you don't need the non-greedy quantifier when using negated character classes, that's the whole point of using them as it means you can avoid the commonly seen .*? pattern.

    I hope this is of interest.

    Cheers,

    JohnGG

      Hello johngg,

      Both excellent points! Thanks for the corrections:

      1:41 >perl -wE "my $s= qq[Can you please remove 'me' as that me is no + longer required, but this 'X' should go as well]; $s =~ s/\s*'[^']*' +//g; say qq[\n$s];" Can you please remove as that me is no longer required, but this shoul +d go as well 1:41 >

      Cheers,

      Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      That did the trick thanks!
Re^2: strip text from a string
by KalaMonkey (Initiate) on Sep 11, 2014 at 15:25 UTC
    Hi,

    Thanks for that, it's put me on the correct track, however when I run it inside a perl script using Starwberry Perl

    #!/usr/bin/perl #use strict; use warnings; $str = "select Assigned, null, null, count(*) from wftask where Assign +ed in ('15000847','20005966','20005965','20004711','15120173','150004 +37','15023846','15022744','15062553','15149541','15000245','15000217' +,'15000803','15000636','15000690','15000437','15001069','15119338','1 +5022744','20009455','20001463','15179195','20008500','20004675','1500 +0988','15179195','15119270') and RefClass is not null and RefKey is n +ot null group by Assigned union select null, null, DistGroup, count(* +) from wftask where DistGroup in ('SLT','Budget Signatory/310','Budge +t Signatory/544','Budget Signatory/539','Budget Signatory/518','Budge +t Signatory/321','Budget Signatory/543','Budget Signatory/338','Budge +t Signatory/513','Budget Signatory/6','Budget Signatory/359','Budget +Signatory/358','Budget Signatory/530','Budget Signatory/300','Budget +Signatory/550','Budget Signatory/490','Budget Signatory/491','Budget +Signatory/376','Budget Signatory/377','Budget Signatory/401','Budget +Signatory/178','Budget Signatory/18','Budget Signatory/236','Budget S +ignatory/40','Budget Signatory/246','Budget Signatory/507','Budget Si +gnatory/475','Budget Signatory/464','Budget Signatory/459','Budget Si +gnatory/18','Budget Signatory/310','Budget Signatory/544','Budget Sig +natory/539','Budget Signatory/518','Budget Signatory/321','Budget Sig +natory/543','Budget Signatory/338','Budget Signatory/513','Budget Sig +natory/6','Budget Signatory/359','Budget Signatory/358','Budget Signa +tory/530','Budget Signatory/300','Budget Signatory/550','Budget Signa +tory/490','Budget Signatory/491','Budget Signatory/376','Budget Signa +tory/377','Budget Signatory/401','Budget Signatory/178','Budget Signa +tory/18','Budget Signatory/236','Budget Signatory/40','Budget Signato +ry/246','Budget Signatory/507','Budget Signatory/475','Budget Signato +ry/464','Budget Signatory/459','North Long Term Allocation','North Lo +ng Term Approvals','North Long Term Review','North Long Term Safeguar +ding','Placement Review','RAS Surgery','SLT Awaiting Allocation','SLT + Duty','SLT Duty Senior','SLT OT Equipment','SLT Pending','SLT Review +','SLT Single Service Reviews','KT26099','Budget Signatory/18') and R +efClass is not null and RefKey is not null group by DistGroup union s +elect null, DistDept, null, count(*) from wftask where DistDept in (' +D3347') and RefClass is not null and RefKey is not null group by Dist +Dept"; $str =~ s/(\s*'[^']*+?')//g; print $str
    I get the message Nested quantifiers in regex; marked by <-- HERE in m/(\s*'^'*+? <-- HERE ')/ at C:\Perl\Perl_tests\SQLTest.pl line 6.

    When I run it on the command line it works fine. As I have said in a another reply, I only started to use perl on Monday please excuse my ignorance.

    Thanks
      Nested quantifiers in regex; marked by <-- HERE in m/(\s*'^'*+? <-- HERE ')/ at ...

      The quantifier sequence  *+? is not valid and is termed 'nested'. See johngg's reply.

      Update: See also Quantifiers in perlre. See in particular the paragraph beginning "Note that the possessive quantifier modifier can not be be [sic] combined with the non-greedy modifier."

        Darn it! Thanks much for the correction; I missed the regex used.

      ,p>I did not care to read the whole SQL statement. Most likely there may be character(s) in the query that are significant (meta characters) for correct regex. So try to escape the string either via quotemeta or \Q & \E.