java - Regex to find variables and ignore methods -


i'm trying write regex finds variables (and variables, ignoring methods completely) in given piece of javascript code. actual code (the 1 executes regex) written in java.

for now, i've got this:

matcher matcher=pattern.compile(".*?([a-z]+\\w*?).*?").matcher(string); while(matcher.find()) {     system.out.println(matcher.group(1)); } 

so, when value of "string" variable*func()*20

printout is:

variable func 

which not want. simple negation of ( won't do, because makes regex catch unnecessary characters or cuts them off, still functions captured. now, have following code:

matcher matcher=pattern.compile(".*?(([a-z]+\\w*)(\\(?)).*?").matcher(formula); while(matcher.find()) {     if(matcher.group(3).isempty()) {         system.out.println(matcher.group(2));     } } 

it works, printout correct, don't additional check. ideas? please?

edit (2011-04-12):

thank answers. there questions, why need that. , right, in case of bigger, more complicated scripts, sane solution parsing them. in case, however, excessive. scraps of js i'm working on intented simple formulas, (a+b)/2. no comments, string literals, arrays, etc. variables , (probably) built-in functions. need variables list check if can initalized , point (and initialized @ all). realize of can done manually rpn (which safer), these formulas going wrapped bigger script , evaluated in web browser, it's more convenient way.

this may bit dirty, it's assumed whoever writing these formulas (probably me, of time), knows doing , able check if working correctly.

if finds question, wanting similar, should risks/difficulties. do, @ least hope ;)

taking sound advice how regex not best tool job consideration important. might away quick , dirty regex if rule simple enough (and aware of limitations of rule):

pattern regex = pattern.compile(     "\\b     # word boundary\n" +     "[a-za-z]# 1 ascii letter\n" +     "\\w*    # 0+ alnums\n" +     "\\b     # word boundary\n" +     "(?!     # lookahead assertion: make sure there no...\n" +     " \\s*   # optional whitespace\n" +     " \\(    # opening parenthesis\n" +     ")       # ...at position in string",      pattern.comments); 

this matches identifier long it's not followed parenthesis. of course, need group(0) instead of group(1). , of course matches lots of other stuff (inside strings, comments, etc.)...


Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -