0

I need some regex help.

What im trying to make is a function block parsing pattern.

the functions look like this:

fn name() {
    contents
}

fn name2() {
    contents2
}

I use this pattern to parse it: (fn \w+\s*\(.*\)\s*{.+(?<=}))

It works as expected, but instead of matching only to the first }, which should be the end of the "name" function, it keeps going until the last } find in the whole document basically. Can someone help me fix it?

Much appreciated.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Molnár Márk
  • 411
  • 1
  • 6
  • 14
  • 3
    If `contents` can contain `n` sets of paired braces for any `n > 0`, then you cannot properly parse this with regular expressions as it is not a regular language. – CollinD Jan 31 '16 at 22:04
  • Got a solution for that, the problem was only the block parsing. Thanks! – Molnár Márk Jan 31 '16 at 22:13
  • For me this looks like a problem where a lexical analysis pattern would be a better solution over regular expressions. – ferdy Jan 31 '16 at 22:19
  • yup. Its a small transpiler to practice regex, i chose this method because im not familiar with lexical analysis stuff. – Molnár Márk Jan 31 '16 at 22:22

1 Answers1

3

With .* and .+ you are matching any character greedily, that's why you consume everything up to the last } character. Change .* to .*? and .+ to .+?

Demo: https://regex101.com/r/pC0hF0/1

While this fixes your temporary problem, also note the comment by @CollinD about nested braces.

timgeb
  • 76,762
  • 20
  • 123
  • 145