0

Given bar(alvin the chipmunk dude) and chipmunk(alvin the chipmunk dude), how would you match the word "chipmunk" only on the "bar" function?

Another question I just asked, but without the needed complexity I was looking for, is answered here. I do not believe this is a duplicate given the answer to the question from @revo. That answer does answer the other question however I see no way to adapt it to ensure the match is contained within two different strings ("bar(" and ")").

chipmunk(?=[^\)\(\\]*(?:\\.[^\)\(\\]*)*\)) (courtesy of @revo) matches "chipmunk" inside of the parentheses, but I want to constrain it to only to to being within "bar(" and ")".

Test here.

Using JetBrains IDE which uses Java.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
forrestmid
  • 1,494
  • 17
  • 25
  • [Capture group 1](https://stackoverflow.com/a/17969620/3103891) with [`(?:bar.*)(chipmunk)`](https://regex101.com/r/gVVt5e/5) – GalAbra Feb 20 '18 at 18:29
  • 2
    Yes, something like `(?<=bar\([^()]{0,1000})chipmunk` will most probably be enough. – Wiktor Stribiżew Feb 20 '18 at 18:42
  • https://stackoverflow.com/questions/48890297/regex-match-a-character-set-within-brackets-containing-other-characters - very similar to your other question. I would suggest you try learning a little more about regex and how to use it. Play around with online regex IDEs and read Java's regex documentation – ctwheels Feb 20 '18 at 19:02
  • Actually, it is not quite clear what exactly you need. And in the previous post, you have all the hints to answer this question. Perhaps, you may also want to use `bar\([^()]*chipmunk[^()]*\)` here. – Wiktor Stribiżew Feb 20 '18 at 19:06
  • `"chipmunk" only on the "bar" function` Whoa, you need a language parser, not a regex. What language does the _function_ come from ? Language's can hide things in comments, string literals, all sorts of places...., not to mention balanced syntax.. –  Feb 20 '18 at 19:07
  • @WiktorStribiżew That first answer works. Please post an answer so I can award credit. Part of my issue is my lack of regex knowledge. I spent about two hours prior to posting my first question and am excited to have learned so much however this lookback functionality in java is much more confusing than the JS regex I was playing with in the online editors. – forrestmid Feb 21 '18 at 15:20
  • @sln I was simplifying my question for SO. In reality the function is much more complicated and is syntactically correct. This is just a string for the sake of regex. – forrestmid Feb 21 '18 at 15:22
  • `I was simplifying my question for SO. In reality the function is much more complicated and is syntactically correct. This is just a string for the sake of regex` What I said in my comment went right over your head .... –  Feb 22 '18 at 17:57

1 Answers1

1

Since you are using a Java regex library, you may leverage the constrained-width lookbehind feature:

Java accepts quantifiers within lookbehind, as long as the length of the matching strings falls within a pre-determined range. For instance, (?<=cats?) is valid because it can only match strings of three or four characters. Likewise, (?<=A{1,10}) is valid.

You may use

(?<=bar\([^()]{0,1000})chipmunk

It matches any chipmunk string that is immediately preceded with bar( followed with 0 to 1000 chars other than ( and ).

You may test it at RegexPlanet.com.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563