Match a regular expression against any non-character or number
Ok, here again. I'll promise to study deeply the regular expression soon :P
Language: PhP
Problem: Match if some badword exist inside a string and do something. The 开发者_运维知识库word must be not included inside a "greater word". I mean if i'll search for "rob" (sorry Rob, i'm not thinking you're a badword), the word "problem have to pass without check.
I'd googled around but found nothing good for me. So, I thought something like this:
If i match the word with after and before any character of the following:
- .
- ,
- ;
- :
- !
- ?
- (
- )
- +
- -
- [whitespace]
I can simulate a check against single word inside a string.
Finally the Questions:
- There's a better way to do it?
- If not, which will be the correct regexp to consider [all_that_char]word[all_that_char]?
Thanks in advance to anyone would help! Maybe this is a very stupid question but today is one of that day when move our neurons causes an incredible headache :|
Look up \b
(word boundary):
Matches at the position between a word character (anything matched by \w) and a non-word character (anything matched by [^\w] or \W) as well as at the start and/or end of the string if the first and/or last characters in the string are word characters.
(http://www.regular-expressions.info/reference.html)
So: \brob\b
matches rob
, but not problem
.
You can use \b
, see Whole word bounderies.
精彩评论