-1
struct mbuh
(

lineshg = 0,
tres = "text",

    fn track all:#() = 
        (
        local c -- clone
            local lpoint = [0,0,0] -- previous point created
            local ccoll = #() -- array for created objects
            local prev = [0,0] -- var for alt distance changing
        ),

    fn listzfun trs:123 x:#() = 
        (
        obj = snapshotasmesh a
            min_vz = max_vz = in coordsys world (getvert obj 1).z
            min_vx = max_vx = in coordsys world (getvert obj 1).x
            min_vy = max_vy = in coordsys world (getvert obj 1).y
        )
)

fn brung man: =
(

)

as the example above , I want to find word "struct xx" & "fn xxx" but only "fn xxx" that inside struct scope not outside it ["fn brung xxx"], I use this regex to find it

^.*?(\bstruct\s*\w+\b)|^.?(\bfn\b+[\w\s:#()""]*)

but its search all fn word inside and outside struct scope, so the question is how can I find word fn xxx that only inside struct scope which is in example above [fn track xxx and fn listzfun xxx] ?

Thank you very much.

CSharpie
  • 9,195
  • 4
  • 44
  • 71
  • 2
    I do think that [regex is _not_ suitable for parsing source code](http://stackoverflow.com/a/19639012/107625). You should [use a lexer](http://parsingintro.sourceforge.net/#contents_item_5) instead. – Uwe Keim Feb 05 '17 at 08:50
  • 1
    While it's possible to do this with Regex, stuff like parenthesis/brackets balancing is very tricky to do with just Regex alone. I'd recommend doing another form of parsing on this first if nothing else. – Abion47 Feb 05 '17 at 08:52
  • But is it hard to do that? as additional info ,each "fn" inside struct separated by coma, while "fn" outside it does not separated by coma. thanks – fajar vashra Feb 05 '17 at 09:04

2 Answers2

0

You can try it like so with this regex:

(struct\s*\w+.*?(?=fn))(fn.*?(?=\),)\)).*?(?=fn)(fn.*?\))(?=\R\))

Demo: https://regex101.com/r/zGAt4D/9

Mohammad Yusuf
  • 16,554
  • 10
  • 50
  • 78
0

I got the same equation as the same as MYGz does but its in c#

@"\bstruct\s*\w+\s
\(
(?>
[^()]+
|
\((?<Depth>)
|
\)(?<-Depth>)
)*
(?(Depth)(?!))
\)
"

Hope this help someone else in the future.