0

I want to make a regular expression to match a string like :

Function_Name ( 'parameters' ) Function Description

so I get the name, parameters and description.

user4157124
  • 2,809
  • 13
  • 27
  • 42
K7rim
  • 147
  • 10
  • 2
    Which language? Perl regex? sed regex? PHP regex? What does it look like for multiple parameters? – cmbuckley Feb 15 '12 at 16:23
  • i think it doesn't make big difference ,does it? – K7rim Feb 15 '12 at 16:29
  • 1
    It makes a difference to how accurate your answer is. Different languages support different regular expression features. Also, the formatting of multiple parameters affects how the regex should capture that part. – cmbuckley Feb 15 '12 at 16:35
  • i meant it would be easy to move for one LANG to another when the I get the general idea sorry for confusion – K7rim Feb 15 '12 at 16:38
  • the parameters are separated by , – K7rim Feb 15 '12 at 16:39
  • 1
    When you realise you need to add more information, [edit the question](http://stackoverflow.com/posts/9297062/edit) instead of tacking on information bit by bit in comments. – Samuel Harmer Feb 16 '12 at 15:37
  • 1
    Don't forget to accept an answer, if it proved valuable to helping in your problem. – Manuel Ferreria Feb 20 '12 at 18:02

3 Answers3

4

Assuming parameters are seperated by , Following PCRE regular expression should work for you.

(\w+)\s+\(([^\)]+)\s+(.+)

Subgroup 1 > function name
Subgroup 2 > parameters
Subgroup 3 > function description

Shiplu Mokaddim
  • 56,364
  • 17
  • 141
  • 187
  • This is a better answer, it has better subgroups. – Manuel Ferreria Feb 15 '12 at 16:43
  • @K7rim This seems to capture the essence of the task (functions declared in source code). Just check that it doesn't capture other things of your source code (not functions) that have the same structure! – heltonbiker Feb 15 '12 at 17:15
  • @Shiplu this ok but with one modification the 2nd subgroup should be like that ([^\)]+) the + sign for one or more characters – K7rim Feb 15 '12 at 18:12
  • also the teh description may have characters like that ' in don't or quotes inside it so i replaced the 3rd subgroup with (.+) – K7rim Feb 15 '12 at 18:13
  • @heltonbiker I need that to capture an autocomplete api file with the syntax like above thank you for your warning! – K7rim Feb 15 '12 at 18:15
  • @K7rim thanks. I should have considered those. Updated thneanswer – Shiplu Mokaddim Feb 15 '12 at 19:27
2

If you are using AutoIt, as the tags suggest, the pattern should be a PCRE.

(\w+)\s*\(\s*([^)]+)\s*\)\s*(.+)

This is a slightly modified version of the pattern Shiplu suggested. It has optional spaces so they don't end up in the match and the same goes for the closing parentheses. There is also no need to escape the parentheses inside a character set.

  • ( start capturing group 1
  • \w+ match one or more word characters (alphanumeric and underscore)
  • ) close capturing group 1
  • \s* match zero or more whitespace characters
  • \( match an opening parantheses
  • \s* match zero or more whitespace characters
  • ( start capturing group 2
  • [^)]+ match one or more characters that are not a closing parentheses
  • ) close capturing group 2
  • \s* match zero or more whitespace characters
  • \) match a closing parentheses
  • \s* match zero or more whitespace characters
  • ( start capturing group 3
  • .+ match one or more characters that are not a whitespace
  • ) end capturing group 3
Robjong
  • 375
  • 1
  • 6
1

I am writing Python Regex, but I don't think it would be hard at all to pass it to PHP or Perl.

My guess would be something like this, but I am uncertain on the second .*, if it will capture correctly the EOL or not.

(\w+)\s\(\s'\s(.+)\s'\s\)\s(.*)
Manuel Ferreria
  • 1,216
  • 1
  • 13
  • 23
  • 1
    are you sure with the {} to capture? from perl I know only (). you also forgot to take the inner whitespaces out with \(\s+' – Hachi Feb 15 '12 at 16:27