Home > database >  Regex to check a string is a valid function prototype
Regex to check a string is a valid function prototype

Time:11-02

I am finding regex pattern to check whether a string is a function prototype or not.

Examples: Valid

foo(var1, var2)

baz()

Examples: Invalid

foo(var1, var2)( - Invalid "(" at the end

foo((var1), var2) - Nested parantheses

baz()() - Only match 1 pair of parantheses

So far, I got this pattern \(([^)] )\)

CodePudding user response:

Try

^\w \([^()]*\)$

See live demo.

This matches words characters, an opening bracket, 0-n non bracket chars and a closing bracket.

Adding start and end anchors requires that the whole input match.

  • Related