How to match exact word anywhere in string with PHP regexp

php regex cheat sheet
javascript string contains "exact match"
php preg_match array of patterns
php find exact match in string
php regex capture
php regex extract
php grep string
preg_match online

I want to find out if user has used the words admin or username anywhere in their possible username string.

So if user wants to use admin, or I am admin, or my admin name or my username, as their username, I want my regexp to detect it. If he uses adminmember I can live with that, don't detect it. I also want detection to be case insensitive.

So far I got this, but it is not really working as I thought, since it will detect words that it shouldn't:

/^[admin|username]+$/i

This will match even adm, and it shouldn't. I have tried with word boundaries, but I couldn't make it work either, maybe I did something wrong. As you can see in my question I would like to detect word admin anywhere in the string, but if it is a part of some word I can skip it.

Thanks in advance.

Square brackets in a regexp are not for grouping, they're for specifying character classes; grouping is done with parentheses. You don't want to anchor the regexp with ^ and $, because that will only match at the beginning and end of the string; you want to use \b to match word boundaries.

/\b(admin|username)\b/i

regex, As you can see in my question I would like to detect word admin anywhere in the string, but if it is a part of some word I can skip it. Thanks in advance. php regex. I'm having some trouble using Regex to match an exact string (and only that string, must not contain prefix or suffix) out of a sample with only slight variations. I've looked over every "duplicate" this has been compared to, and none of the solutions seem to apply to the problem I'm trying to accomplish.

Just look for words using word boundaries:

/\b(?:admin|username)\b/i

and if there is a match return error e.g.

if (preg_match('/\b(?:admin|username)\b/i', $input)) {
    die("Invalid Input");
}

Need "exact match within string" - PHP, I need to find exact matches for search terms within a string. EG "cat" will find cat http://php.net/preg_match. @ Ize Your code doesn't work with the requirements, it matches every word but candle when it should not as stated above. This will return all the text id which cam is found anywhere in the single . Hi, In my recent grails project, i needed to check the occurrence of a word in a string. I could have used contains() method of groovy , but the problem with contains() is that it also matches the word if the word is contained as a substring in any word. I searched a lot, but nothing worked out so i decided to use Regular expression for it. With the help of my colleague,we came out with a

Try the below snippet to keep your list of words in Array.

$input = "im username ";
$spam_words = array("admin", "username");
$expression = '/\b(?:' . implode($spam_words, "|") . ')\b/i';

if (preg_match($expression, $input)) {
  die("Username contains invalid value");
}
else {
  echo "Congrats! is valid input";
}

Working Fiddle URL: http://sandbox.onlinephpfunctions.com/code/6f8e806683c45249338090b49ae9cd001851af49

Regex to match exact word in a string, Hi, In my recent grails project, i needed to check the occurrence of a word in a string. I could have used contains() method of groovy , but the  Checking for (exact) equality should really be done without regex. – Bart Kiers Apr 22 '11 at 6:26 Because we look up how to replace text in a Bash script and get an answer that involves Perl and regex. – sudo Aug 1 '15 at 19:11

This might be the pattern that you're looking for:

'#(^|\s){1}('. $needle .')($|\s|,|\.){1}#i'

Some details depend on the restrictions that you want to apply.

preg_match_all - Manual, preg_match_all — Perform a global regular expression match. Description ¶. preg_match_all ( string $pattern , string $subject [, array &$matches [, int $flags  I'm attempting to get the exact match of a word, but I just can't seem to get it. I have never made a foray into regular expressions before, so this is all brand new to me. I want to search the string, see if an exact word is in there, and only return a positive index if that exact word is there.

Regex Tutorial - \b Word Boundaries, How do you match a specific word in a regular expression? Plenty of people want to match an exact word or phrase, but I seem to be the only one who wants to match only one exact word or phrase. If this CAN be expanded to a phrase that would be perfect. Assume: Go for a hikeGo for a hike, on a mountain. I want to go for a hike.

PHP, (regexes) are a sequence of characters describing a special search pattern in the form of text string. They are basically used in programming world algorithms for matching some loosely defined patterns to achieve some relevant tasks. You should use re.search here not re.match.. From the docs on re.match:. If you want to locate a match anywhere in string, use search() instead. If you're looking for the exact word 'Not Ok' then use \b word boundaries, otherwise if you're only looking for a substring 'Not Ok' then use simple : if 'Not Ok' in string.

PHP Regular Expressions Tutorial: Preg_match, Preg_split , to perform a pattern match on a string. It returns true if a match is found and false if a match is not found. Start phpsh, put some content into a variable, match on word. The preg_match method used the PCRE engine within the PHP language to analyze variables: $content1, $content2 and $content3 with the (\w)+ pattern. $content1 and $content2 contain at least one word, $content3 does not.

Comments
  • username will be with spaces. hope its without spaces
  • @Venkatraman I am allowing spaces, why do you think it is bad ?
  • if its user display name is fine but if its for user unique identification is not good in DB Level indexing
  • ID is for user unique identification
  • then fine. check my answer for keeping the admin, username in the array in future. its bit easy to add one more element or get those list of words from external file / api. with working PHP Fiddle URL
  • Yeah, this works exactly as I want. Thanks for explanation too.
  • Author said he wants case-sensitive so you should remove trailing I
  • What question are you answering?