The leading $and the middle \w are common between patterns. In fact, all that's required is to require a closing ) only when there was an opening (.
Using a less well known feature
\$ # Our '$' prefix
(?:
# Optionally *don't* find the opening paren.
|
(\() # Optionally find the opening paren
)
(\w+) # The middle part is captured in $2
(?(1)\)) # Require a closing paren only if $1 matched.
Merely removing the prefix
\$ # Our '$' prefix
( # Capture into $1
(?:
\w+ # Plain word.
|
\( \w+ \) # A word with parentheses around it.
)
)