---
title: "检查不包含重复字符的正则表达式"
author: "Goro"
date: "2012-08-15T16:31:57Z"
tags: []
categories: ["编程语言"]
slug: "untitled"
draft: false
lastmod: "2012-08-15T16:33:32Z"
id: 1633003
url: "https://goro.iteye.com/blog/1633003"
---

以下可以根据需要修改。


```java

//匹配不包含重复字母
String regex = "^[a-z]*([a-z])[a-z]*\\1[a-z]*$";
System.out.println(!"ABCDEF".matches(regex));
System.out.println(!"ABCDEA".matches(regex));
System.out.println(!"A".matches(regex));

//匹配不包含连续重复字母
regex = "^[a-z]*([a-z])\\1[a-z]*$";
System.out.println(!"ABCDEF".matches(regex));
System.out.println(!"ABCDEA".matches(regex));
System.out.println(!"ABBCDDEF".matches(regex));
System.out.println(!"A".matches(regex));

```


输出

```java

true
false
true
true
true
false
true

```