As I understand it, the matching problem you're posing is to detect a pattern with duplicates like .abc(abc_p) and then make multiple changes to the duplicated alphabetic substrings, so that the string above becomes the two strings .abc_1(abc_p_1) and .abc_2(abc_p_2). The abc and abc_p substrings may occur in any order, as may the dotted and parenthetic fields.

I would approach the problem in two steps: detect duplication; transform all duplicated substrings.

From previous postings, I think the elements the detection regex might be
    my $rx_pre_dup = qr{ [.(] }xms;
and
    my $rx_str = qr{ \b (?: abc_p | abc) \b }xms;
which can be combined into the regex
    my $rx_dup_str = qr{ $rx_pre_dup $rx_str }xms;

Then a match for duplicates might look like
    m{ $rx_dup_str .*? $rx_dup_str }xms
and one can exercise this with

Win8 Strawberry 5.8.9.5 (32) Tue 04/06/2021 13:52:38 C:\@Work\Perl\monks\suvendra123 >perl use strict; use warnings; my $rx_pre_dup = qr{ [.(] }xms; my $rx_str = qr{ \b (?: abc_p | abc) \b }xms; my $rx_dup_str = qr{ $rx_pre_dup $rx_str }xms; for my $string ( '.abc', '.abc(pqr)', '.abcfoo(abc)', '.abc.abc', '.abc(abc_p)', '.abc_p.abc', '.abc_p.abc(abc_p)', '.abc_p (abc_p)', ) { my $got_dups = $string =~ m{ $rx_dup_str .*? $rx_dup_str }xms; printf "'$string' has %sdups \n", $got_dups ? '' : 'NO '; } ^Z '.abc' has NO dups '.abc(pqr)' has NO dups '.abcfoo(abc)' has NO dups '.abc.abc' has dups '.abc(abc_p)' has dups '.abc_p.abc' has dups '.abc_p.abc(abc_p)' has dups '.abc_p (abc_p)' has dups

Some questions:

Once you know that a string has dups, you can find all $rx_dup_str matches and make substitutions as needed. The int abc; transformations should be fairly easy, and I think you already have those in hand.


Give a man a fish:  <%-{-{-{-<


In reply to Re^3: Not getting proper output value by AnomalousMonk
in thread Not getting proper output value by suvendra123

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.