Escherichia coli strains belonging to O-serogroup 138 and 139 are important as disease agents in pigs causing post-weaning diarrhea and edema disease. Several types of shiga toxin-producing O138 and O139 strains were isolated from diarrheic humans and from cattle and food of bovine origin. Serotyping is the current method for detection of O138 and O139 strains but its applicability can be limited due to the presence of capsules and capsular-like bacterial surface antigens and in the case of rough LPS. To overcome these difficulties for diagnosis, we have developed a specific PCR method suitable for detection of different types of O138 and O139 strains. The O-antigen gene clusters of E. coli O138 and O139 type strains were sequenced, and the genes were identified on the basis of homology. By screening against 186 E. coli and Shigella type strains, two genes specific to each of E. coli O138 and O139 were identified, respectively, and were tested on 15 clinical and environmental isolates of those two serogroups in a double-blind test. The sensitivity of the PCR assays was determined, and the detection limits were 2pg per μl of chromosomal DNA and 2CFU per 10g of water or pork samples. PCR-based detection of O-antigen specific genes of E. coli O138 and O139 was shown to be accurate, highly sensitive and rapid, and is suggested as a new diagnostic tool for investigations of infections and outbreaks with these strains in animals and humans and for control of food.