tags:

views:

138

answers:

3

How to match a string which doesn't contain a dot (.) using regular expression ?

+8  A: 

why regex??

$str="string_with_no_dots";
if (  strpos ($str,"." ) === FALSE ){
 print "ok, no dots\n";
}
ghostdog74
Why? Smells homework assignment long way.
anddoutoi
because of url rewrite
ivan73
+3  A: 

Create a class matching anything except the specified, which is done using [] with the ^ operator. And, as the comments say, you want to match it against the beginning (^) and the end ($) or the string so that we check the entire string.

^[^.]*$
Mats Fredriksson
Since . has no special meaning within a class you don't need the \ there.
VolkerK
good point, I'll edit the answer.
Mats Fredriksson
You probably want `^[^.]*$`, to avoid partial matching.
Kobi
do you even need the * ?
Colin Newell
@Colin Newell: Yes, because this will otherwise match the first character that isn't a `.` and return true.
R. Bemrose
I used Kobi's solution, dollar sign is very important there :)
ivan73
A: 

Use this regular expression :

if ( !preg_match ( '/\./',$str ,$val ) )

{

print "ok , no dots\n";

}

pavun_cool