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

hi i had string which is the contents of a file and i need a regular expression to match and replace the following pattern.

string:
OT; some line /*asas*/ ##here comments should be removed /*asdas*/ #OT;.....RT;should be replaced with OT;.....DT;OT.......RT; some line RT; some lines somlelines OT; some line /*asas*/ /*asdas*/ some line RT; what i need is like OT; some line1 some line2 DT; OT; some line1 some line2 RT; some lines3 somlelines4 OT; some line5 some line6 DT: OT; some line5 some line6 RT; if($string=$string=~ s/OT;(.*?)(/*(.*?)*/)*(.*?)RT;/OT;$1$4DT;\nOT;$1$ +4RT;/gsr) { print "modified string".$string; }

please help me out

Replies are listed 'Best First'.
Re: Need regular expression (OP needs to read the docs)
by ww (Archbishop) on Apr 14, 2015 at 15:35 UTC

    You "need" to help yourself. Read Markup in the Monastery AND perldoc perlretut and perldoc perlre. This is NOT code-a-matic -- even when you post unintelligible nonsense.

    Please format each code, data and sample output within a SEPARATE <c> </c> (code) block to make your question easy to understand.

    Your attempt at substitution is

    1. wrong
    2. un-neccessarily complex
    3. incomplete (post compileable code; show how you're populating $string).

    UPDATE: Fixed borked codetag example. TY, MLX!

Re: Need regular expression
by hdb (Monsignor) on Apr 14, 2015 at 14:29 UTC

    The flip-flop operator can help:

    use strict; use warnings; while(<DATA>){ next if m|^/\*.*\*/|; print if /^OT;$/../^RT;$/; } __DATA__ OT; some line /*asas*/ ##here comments should be removed /*asdas*/ #OT;.....RT;should be replaced with OT;.....DT;OT.......RT; some line RT; some lines somlelines OT; some line /*asas*/ /*asdas*/ some line RT;
Re: Need regular expression
by ww (Archbishop) on Apr 14, 2015 at 16:51 UTC

    2nd comment since OP has not responded to the previous nodes

    #! /usr/bin/perl -w use 5.018; my $string = "OT; some line /*asas*/ # here C-style comments should be removed /*asdas*/ #OT;.....RT;should be replaced with OT;.....DT;OT.... +...RT; *note1 some line RT; some lines somlelines OT; another line /*asas*/ /*asdas*/ yet another line RT;"; =head first, my note: # /me does not understand WTF the OP's comment above means and now, OP's spec: what i need is like OT; some line1 some line2 DT; OT; some line1 some line2 RT; some lines3 somlelines4 OT; some line5 some line6 DT: OT; some line5 some line6 RT; =cut my $regex1 = qr([/][*]); my $regex2 = qr([*][/]); $string =~ s/$regex1.+?$regex2//gs; say $string; =head output C:\> junk_check.pl OT; some line # here C-style comments should be removed #OT;.....RT;should be replaced with OT;.....DT;OT.......RT; *n +ote1 some line RT; some lines somlelines OT; another line yet another line RT; C:\> =cut

    The above does part of what you ask, without deleting the Perl-style comments, but /me does NOT understand your spec re inserting DT; (or DT: as in an instance in your example). Perhaps, if not in your own interest, in the interest of future readers, you will clarify the intent (and the probable typo) by an explicit explanation of when DT; should be inserted.


    If I've misconstrued your question or the logic needed to answer it, I offer my apologies to all those electrons which were inconvenienced by the creation of this post.