php - Regex match string starting with dollar sign and contains ( and no spaces or hyphen

Trying to get a regex that matches
$hey()
$hey('hi')
But not:
$hey->hi()
$hey moretext()
Needs to start with a dollar sign and have a ( and no spaces or dashes.
My idea was:
^\$.*\[^- ]$
Basically trying to find all the variable functions in my PHP code base.
Answer
Solution:
This should do the job.
\w+ will match all valid variable names, ( and ) are required. Anything (or nothing) is allowed between the ().
^\$\w+\(.*\)
Answer
Solution:
PHP has a built-in lexer that is better (more accurate) than any regexp:
$tokens = token_get_all('<?php $var; $bar("hi"); $tar->(123);');
foreach ($tokens as $index => $token)
{
$token_next = $tokens[$index + 1] ?? null;
if (is_array($token) and $token[0] === T_VARIABLE and $token_next === '(')
{
var_dump($token[1] . $token_next);
}
}
Keep in mind that you are wrong about the pattern since these lines are valid PHP code:
$var ();
$var\n\n();
${' aaaaaa'}\n\n();
${" aaaaaa$bar->bbbbbb"}\n\n();
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: your lock file does not contain a compatible set of packages. please run composer update
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.