1

I need to match any characters before the first encountered specific character. Tried:

use feature 'say';
say $1 if 'bla-bla if (func(arg1, arg2) < 0) {' =~ /func\s*\((.*?)\)\s*\{/;

I assumed that there will be no output for this code, cause the .*? matches arg1, arg2 (i.e. before the nearest ) ) and there is no { after func(arg1, arg2) so the regex turns to false. However, the output was arg1, arg2) < 0. Indeed, non-greedy regex chooses as few as needed to allow the overall pattern to match. But I want this regex to be false with such strings. I found in that post the working solution. In my case the .*? should be changed to [^\)]*. But is there any other ways to do it? Thanks for any clarifications and examples. Sorry if this is obvious

Toto
  • 89,455
  • 62
  • 89
  • 125
z0lupka
  • 236
  • 4
  • 19
  • To clarify, you want to capture `arg1, arg2` instead of `arg1, arg2) < 0`, correct? – ThisSuitIsBlackNot May 04 '16 at 22:52
  • 1
    Hang on, so you have a good solution? Are you asking simply whether there are yet other ways? – zdim May 04 '16 at 22:53
  • I don't want to capture. >I want this regex to be false with such strings – z0lupka May 04 '16 at 22:54
  • @zdim I have no other solutions, so I would be grateful for any examples – z0lupka May 04 '16 at 22:55
  • "I want this regex to be false with such strings" So you want to write a regex that *doesn't* match `bla-bla if (func(arg1, arg2) < 0) {`? In that case, what *do* you want to match? – ThisSuitIsBlackNot May 04 '16 at 22:56
  • @ThisSuitIsBlackNot I want to match strings like `func (...) {}` but not others – z0lupka May 04 '16 at 22:58
  • 1
    Your question states that you _do get_ that using `[^)]` ... ? Am I misunderstanding it? (Btw, that is the first thing I reach for when I need a non-greedy match.) – zdim May 04 '16 at 23:00
  • 1
    Help me out here, I don't understand. Try this: `perl -wE '$str = "(func(arg1, arg2) < 0) {"; if ($str =~ m/func\s*\(([^)]*)\)\s*\{/) { say "Got: $1" } else { say "nope" }'`. I get `nope` as it stands, meaning that the match fails. If I remove the " < 0)" part then I get `arg1, arg2`. Remove the capturing parenthesis if you don't need to remember the match itself. – zdim May 04 '16 at 23:05
  • Thanks @zdim. It seems to me that there is another way to specify my regex. But `[^)]` suits me of course. I don't need to capture something, it's just for example – z0lupka May 04 '16 at 23:05
  • 1
    Ah, OK. The `[^char]` is my preferred way to do non-greedy matching. Taken together with `.*?` (and friends) there is no reason to ask for yet another, in my opinion. I think that there are no other idioms (or simple ways) for that ... in regex. I suppose you can use `split` instead, where you get that for free. – zdim May 04 '16 at 23:09
  • @zdim Thanks for your clarification. Noted. – z0lupka May 04 '16 at 23:11
  • 1
    @red0ct: Why have you tagged "lookaround"? There are none here. – Toto Oct 03 '19 at 13:43
  • @Toto You're right. Inattentively read, sorry. – red0ct Oct 03 '19 at 13:53

0 Answers0