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

Hi monks, I have a code as follow:
$who = $name || '(unknown)'; $greet = "#{getName($who)}#"; $greet =~ s{#\{(\w+)\(([^\)]+)\)\}#}{$obj->{"$1"}->($2)}eg;

my logic is to look for a ( to start, and read until I found a ) to end. This will be no problem if $name is in a normal status, however, if $name goes to (unknown), then my s regex will not be working. I believe I can make the string become '(unknown\)' can help somehow, but then I have no idea on the regex, while I can looking for ) and ignore \) at the same time.

Please let me know if I didn't state my question making sense. Any clues ?

Replies are listed 'Best First'.
Re: How to cater a self defined escape char in regex ?
by Loops (Curate) on Apr 08, 2013 at 10:07 UTC
    Hi there,

    Have a feeling there is more to the story that what you've revealed in your question. You didn't give us any working code, or even an idea about what "$obj" may be. But the obvious question is, why not just remove the '(' and ')' from $who before proceeding?

      Thanks Loops, this is really a rephrased snip story, which I think to post the whole story only even making what I want to ask become more ambiguous. However, this is a good lesson learned for how to ask more properly.

      If there's a second go, I'd ask my question in this way.

      $str = '#{XX(' . $unknownString . ')}#';

      Then how do I write an regex to get $1 eq $unknownString from $str? Provided the $str was actually reading from a random text file, so I cannot pre-modify it before I run the regex.

Re: How to cater a self defined escape char in regex ?
by hdb (Monsignor) on Apr 08, 2013 at 10:16 UTC

    I assume you want to return either $name or '(unknown)' including the () from your regex.

    $greet =~ s{#\{(\w+)\((\({0,1}\w+\){0,1})\)\}#}{$obj->{"$1"}->($2)}eg;
      BIG THANKS! This inspired a lot, I've modified a little with /#\{(\w+)\((.*)\)\}#/ so it fits my needs perfectly! Sorry I might put my question badly.
Re: How to cater a self defined escape char in regex ?
by aitap (Curate) on Apr 08, 2013 at 12:34 UTC

    Are you sure that your question is not an XY Problem?

    Why do you construct your string in the first place while you can avoid parsing it back using regexes if you make a simple check, like this?

    my $greet = $name ? $obj->{getName}->($name) : "<what you wanted to do + in case of (unknown)>";
    (read Conditional Operator if you don't know what <condition> ? <value> : <value> means)

      Thanks aitap. Honestly, I can't self determine am I asking XY question. If I am asking Y for X, at lease at the the moment I was posting, I believed that I was asking X. Moreover, what is X and which is Y is quite depends on how the reader understand the question. Thaz what I am still learning to understand.

      However, I've so realized that I am putting this question badly. I've tried to rephrase it in this reply. I hope, if you don't mind, please teach me if I am asking the right question this time. Thanks again! =)