diff --git a/src/lib/generateRules.js b/src/lib/generateRules.js
--- a/src/lib/generateRules.js
+++ b/src/lib/generateRules.js
@@ -382,7 +382,11 @@ function* resolveMatchedPlugins(classCandidate, context) {
   const twConfigPrefix = context.tailwindConfig.prefix
 
   const twConfigPrefixLen = twConfigPrefix.length
-  if (candidatePrefix[twConfigPrefixLen] === '-') {
+
+  const hasMatchingPrefix =
+    candidatePrefix.startsWith(twConfigPrefix) || candidatePrefix.startsWith(`-${twConfigPrefix}`)
+
+  if (candidatePrefix[twConfigPrefixLen] === '-' && hasMatchingPrefix) {
     negative = true
     candidatePrefix = twConfigPrefix + candidatePrefix.slice(twConfigPrefixLen + 1)
   }

diff --git a/tests/prefix.test.js b/tests/prefix.test.js
--- a/tests/prefix.test.js
+++ b/tests/prefix.test.js
@@ -358,3 +358,19 @@ it('prefix with negative values and variants in the safelist', async () => {
     }
   `)
 })
+
+it('prefix does not detect and generate unnecessary classes', async () => {
+  let config = {
+    prefix: 'tw-_',
+    content: [{ raw: html`-aaa-filter aaaa-table aaaa-hidden` }],
+    corePlugins: { preflight: false },
+  }
+
+  let input = css`
+    @tailwind utilities;
+  `
+
+  const result = await run(input, config)
+
+  expect(result.css).toMatchFormattedCss(css``)
+})

🐛 Unnecessary classes are output
<details>
<summary>Environments</summary>

**What version of Tailwind CSS are you using?**

v3.0.24

**What build tool (or framework if it abstracts the build tool) are you using?**

postcss v8.4.7, webpack v5.72.0

**What version of Node.js are you using?**

v16.13.2

**What browser are you using?**

N/A

**What operating system are you using?**

macOS
</details>

**Reproduction URL**

- https://play.tailwindcss.com/KvFiBdkhw3  
  To check the output of 2 characters,  
  change `prefix:'tw_-'` to `prefix:'tw-'` in `Config` tab.
- https://play.tailwindcss.com/00iNAwt2JZ

**Describe your issue**

As the title says.
Unnecessary classes should not be output.

tests/prefix.test.js