Reputation: 65
$db = new PDO($dsn, $db_id, $dbpw);
$db= new PDO($dsn, $db_id, $dbpw);
I want to get exactly the variable name '$db'.
Other variable names starting with $db should be excluded.
I wrote the following regular expression:
/(\$db)[^_a-zA-Z0-9]/g
So '$db_id' and '$dbpw' were excluded as I intended.
However, just to the right of $db, one character(space, =, etc.) is matched 'more' like this: '$db ', '$db='
This is not my intention. How can I match exactly '$db' in this situation?
Upvotes: 1
Views: 46
Reputation: 368894
You can use word boundary (\b
):
/(\$db)\b/
According to Regex Tutorial - \b
Word Boundaries:
There are three different positions that qualify as word boundaries:
- Before the first character in the string, if the first character is a word character.
- After the last character in the string, if the last character is a word character.
- Between two characters in the string, where one is a word character and the other is not a word character.
Simply put: \b allows you to perform a "whole words only" search using a regular expression in the form of \bword\b. A "word character" is a character that can be used to form words. All characters that are not "word characters" are "non-word characters".
Upvotes: 1