http://www.perlmonks.org?node_id=11130015

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

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: Please correct me in the above regular expression
by hippo (Bishop) on Mar 20, 2021 at 22:28 UTC
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Please correct me in the above regular expression
by afoken (Chancellor) on Mar 21, 2021 at 15:22 UTC

    Same monk, same problem as Unable to find my mistake and all six other threads started by suvendra123 so far.

    Regular expression to match

    int abc;

    .abc(abc)

    above as input and below as output is

    .abc

    (abc

    abc

    is

    [\.(]abc

    Please correct me

    Please do not open new threads for old problems.

    Show at least a little bit of respect for people trying to help you by reading and understanding the answers you were given before.

    Perlmonks is not a code writing service, you are expected to learn from the answers you were given.

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re: Please correct me in the above regular expression
by AnomalousMonk (Archbishop) on Mar 20, 2021 at 23:24 UTC

    hippo has shown++ the regex you must use to match any or all of the substrings .abc (abc abc in the string "int abc;\n.abc(abc)".

    A narrative explanation may be useful. The regex pattern [\.(] (or [.(] which I will use from here on; the escape is not needed in a character class, but does no harm) defines the characters in a class. The regex [.(]abc requires that a character from the class and the following 'abc' characters all be present for a match to occur.

    ++++------- a single character from this class (an "atom") |||| is required vvvv [.(]abc ^^^ ||| +++---- all these characters are required
    So 'int abc;' cannot match because 'abc' is not preceded by a required "atom": a character from the character class.

    On the other hand, the regex pattern [.(]?abc can match something in 'int abc;' because the character class has been made optional by the ? (0 or 1) quantifier:

    ++++------- a character from this class is an "atom" |||| |||| +++--- all these characters are required |||| ||| vvvv vvv [.(]?abc ^ | +------ 0 or 1 of preceding "atom" is required

    In the context of this discussion, an "atom" can be thought of as anything that can be quantified by the * ? + {} quantifiers; see discussions of quantifiers in Regular Expressions in perlre and Matching repetitions in perlretut.


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