tags:

views:

62

answers:

3

I am getting some problems while trying to use this regex:

(public +function|private +function|function) +([a-zA-Z_$][0-9a-zA-Z_$]*) *\\(([0-9a-zA-Z_$, ]*)\\) *{(.*)}

For some reason, $1 and $2 are returning the same value.

The target string is:

public function messenger(text){
    sendMsg(text);
}
private function sendMsg(text){
    alert(text);
}

How can I fix that? By the way, I am using Javascript.

EDIT:

Ok, the answers worked, but the problem now is that the last paramenter is returning "sendMsg(text); } private function sendMsg(text){ alert(text);}", it is not stopping in the first "}"

+3  A: 

actually, this was wrong, or at least misleading

see http://stackoverflow.com/questions/2588994/regex-is-capturing-the-whole-string/2589470#2589470 for the improved version

| has a very low priority, you probably want

(public +function)|(private +function)|(function)

or even

(public|private +)?function

As a side note, regexp is not the best tool for parsing programming languages' grammars. For example, a regexp solution will fail on something like

var foo = "private function";

stereofrog
Your second regexp is missing a space: it will match "publicfunction" but not "public function".
jemfinch
I am using javascript, there is no other way in it to parse a language
M28
+1  A: 

Would something like this be what you're looking for?

 (public |private +)?function

This will check for either public, private, and the ? signifies those are optional so only function will work too. Just tested it using regexpal.com

Bartek
A: 

Try

(public | private)?\sfunction

Beware tho, this could match: private private public private public function function. If you can assure that the code is "clean" and doesn't have this kind of scenarios then use it.

Ben