-1

My first problem is, I need to search for two consecutive sets of parenthesis, for example, (log dkdkdkd) (log edksks)

This code below solves this first problem:

      ^\([^)]*\) \([^)]*\)$

Second problem, in addition to using the solution above, I need to capture the text after the log something like this:

       ^\(log (.*)\) \(log (.*)\)$

But this above solution does not work because it find more than two sets of parenthesis, for example:

           (log dkdkdkd) (log edksks) (log riwqoq)

What I really need is to find two sets of consecutive parenthesis while capturing the text after the log text?

  • Try adding the word: regex to your question title. You will see a popup with information about how you can learn more about regular expressions, and also about tools like http://regex101.com that can help you interactively develop the expression(s) you need. That way you won't have to ask three questions about the same basic problem: http://stackoverflow.com/questions/32055326/how-to-capture-text-within-a-negate-class-character-using-perl http://stackoverflow.com/questions/31949142/whats-the-regular-expression-to-find-two-sets-of-parenthesis-in-a-row-using-per – dlamblin Aug 17 '15 at 17:26
  • My answer to [your previous question](http://stackoverflow.com/questions/32053450/missing-last-character-in-perl-regex) answered exactly this – Borodin Aug 17 '15 at 20:04

2 Answers2

4

You can tell Perl that the text after "log" doesn't contain (:

/^\(log ([^(]*)\)\s*\(log ([^(]*)\)$/

Or, if ( is possible and you only want to exclude (log, check that in two steps:

for my $s ('(log this) (log matches)',
           '(log these) (log do) (log not)'
          ) {
    my @matches = $s =~ /^\(log (.*)\)\s*\(log (.*)\)$/;
    next if $matches[0] =~ /\(log /; # More than 2 logs, skip.

    say "($_)" for @matches;
}
choroba
  • 200,498
  • 20
  • 180
  • 248
  • Just tested it and your solution works. I have to wait two minutes to accept your answer. – Bruce Walker Aug 17 '15 at 16:40
  • I like your short version solution because I can also use it in notepad++, which allow you to see the solution in real time faster. – Bruce Walker Aug 17 '15 at 16:44
  • @BruceWalker There's better tools than Notepad++ available on Windows to try out a real time solution: http://www.hongkiat.com/blog/regular-expression-tools-resources/ – dlamblin Aug 17 '15 at 17:30
0

You can use a non-greedy search

/^\(log (.*?)\) \(log (.*?)\)/

Saying .*? instead of .* makes the regex engine try to match the pattern with the minimum number of characters necessary.

mob
  • 110,546
  • 17
  • 138
  • 265
  • 1
    Unfortunately, this is exactly the case where non-greedy search doesn't help. – choroba Aug 17 '15 at 16:44
  • I originally tried this solution and it does not work for me.. – Bruce Walker Aug 17 '15 at 16:49
  • The initial solution had a `$` at the end of the regex, which would prevent it from working. But this solution has been edited and works (for me, anyway) now. – mob Aug 17 '15 at 16:50