---
title: "匹配&但不匹配&nbsp;的正则写法"
author: "flysnowxf"
date: "2010-12-24T15:53:13Z"
tags: ["Java"]
categories: ["编程语言"]
slug: "nbsp"
draft: false
lastmod: "2010-12-24T15:53:13Z"
id: 851236
url: "https://flysnowxf.iteye.com/blog/851236"
---

匹配&但不匹配&nbsp;怎么写？

```java
&[^(nbsp;)]
```
这样其实是匹配&加一个字符，比如&1、&a，正确的写法为

```java
&(?!nbsp;)
```

这样不会占一个字符。它的意思为不匹配&nbsp;中的&。
那么匹配&但不匹配&nbsp;或者&lt;的写法为

```java
&(?!nbsp;|lt;)
```