javascript - Using Regex to extract line numbers of GOTOs/GOSUBs -
i'm trying extract gotos/gosubs line numbers basic code. intend use nodejs manage matches (so it's js-flavoured regex).
i'm doing tests using regex101.com (see here https://regex101.com/r/sazuue/2 ) , i'm pretty close want:
- extract
goto ###["goto", " ", "###"] - extract
gosub ###["gosub", " ", "###"] - extract
if (cond) ###["then", " ", "###"] - extract
on err goto #, ##, ###["goto", " ", "#", ", ", "##", ", ", "###"] - same above
on err gosub - deal fact spaces between
goto,gosub,then,,optional or can multiple , in cases return exact number of spaces indicated.
so far have come following regex:
/(goto|gosub|then)(\s*)(\d+)(?:(\s*,\s*)(\d+))*/ig
testing on:
100 on err goto 10000, 30, 200, 10,800: gosub 20: if 10: goto30: goto 50
all matching groups ok except on err goto returns first , last numbers (10000 & 800) , not others.
what missing ? :)
there no way have arbitrary number of captures regex, , there no way access multiple captures within single group js regexp not store capture value stack each group (the subsequent capture re-writes existing one, , thus, last capture stored per group).
capture streak of comma-separated numbers , split them separately. e.g. make end of pattern ((?:\s*,\s*\d+)*) (to match 0+ sequences of , enclosed 0+ whitespaces followed 1+ digits) , then, upon match, split /\s*,\s*/ , filter.
see js demo:
var rx = /\b(go(?:to|sub)|then)(\s*)(\d+)((?:\s*,\s*\d+)*)/gi; var str = "100 on err goto 10000, 30, 200, 10,800: gosub 20: if 10: goto30: goto 50"; var m; while ((m = rx.exec(str)) !== null) { console.log( [m[1], m[2], m[3], m[4].split(/\s*,\s*/).filter(boolean)] ); }
Comments
Post a Comment