26

image Finding Variable Names in Source Code

Sometimes we find ourselves in the middle of a job, unable to sort out a simple syntax problem. Regular expressions can be useful in many ways. If you wanted to locate references to variables in a PHP script, you could use a regular expression in your text editor to search for them. But how do you do that when you don’t know what the variable names are?

This regular expression matches all variable names in PHP. They always begin with a dollar and then a letter, but never a number:

\$[a-zA-Z][a-zA-Z0-9]*

Note the two ranges, the second one being repeated as many times as required until we reach a nonalphanumeric ...

Get Developing Quality Metadata now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.