Remove Punctuation in Java Except Apostrophes

Learn how to strip punctuation from text in Java while keeping apostrophes, with practical examples for cleaning captions, transcripts, and audio or video metadata.

Before you start

This task runs locally in Java, so there is no cloud service, account, or upload step unless you add one yourself. You need a Java project with a string of text to clean, such as a video title, file name, caption, or transcript line. The examples use standard java.lang and java.util.regex features, so no third-party library is required.

Decide which apostrophe characters you want to keep. In practice, text can contain the straight apostrophe/typewriter apostrophe, the typographic right single quotation mark, or another Unicode punctuation character used as an apostrophe. A reliable rule is to preserve the characters your content actually uses while removing the rest.

Step-by-step instructions

Use a regular expression and String.replaceAll to remove punctuation except the characters you define as apostrophes. Start with an explicit character class so the behavior is easy to read and change.

Keep the straight apostrophe only

Create a pattern that matches punctuation but excludes the straight apostrophe. The expression below removes punctuation using \p{Punct} with a negative lookahead to keep that one character.

String cleaned = input.replaceAll("(?=)\p{Punct}(?<!')" , "");

This keeps the straight apostrophe and removes characters such as periods, commas, question marks, exclamation points, colons, semicolons, quotation marks, parentheses, brackets, and hyphens that are covered by the punctuation classification.

Keep straight and curly apostrophes

When your text is pasted from a word processor or subtitle editor, it may contain the right single quotation mark (U+2019) as an apostrophe. Add that character to the excluded set.

String cleaned = input.replaceAll("[\p{Punct}&&[^'’]]", "");

The intersection syntax first selects all punctuation and then excludes the straight apostrophe and U+2019. Keep this combined version when you do not control the input source and want to handle both common forms.

Preserve matching single quotes

If you only want to keep apostrophes that look like paired contractions, preprocess the text before stripping punctuation. Replace the apostrophe-like characters you want to retain with a temporary non-punctuation token, run the punctuation removal, then restore them.

For example, replace every apostrophe with a placeholder such as @@APOS@@, remove punctuation with replaceAll("\p{Punct}", ""), and then replace the placeholder with the apostrophe again. This is also useful when you need to protect a different punctuation mark such as a decimal point in numbers.

Check the Java version only if you are using older code. replaceAll and \p{Punct} work in long-supported Java releases, but if a legacy runtime rejects Unicode escapes or curly apostrophe literals, store the characters as escaped Unicode values and verify the source file encoding is UTF-8.

Check the output

Run a short console example with test text that includes a contraction, a possessive, a question mark, a comma, and a curly apostrophe if you are preserving it.

The expected output should keep “don’t” as a connected word and turn “Stop, look, and listen!” into “Stop look and listen” or similar, depending on your pattern. Verify that removed punctuation does not leave double spaces. If spacing matters for a search field, file name, or closed-caption line, add a cleanup step that replaces multiple spaces with one and trims the result.

This approach only removes the Unicode punctuation category. Symbols such as mathematical operators, currency signs, emoji, or non-Latin punctuation may require a broader custom character class. Numbers with a hyphen or minus sign may also be changed because the hyphen is punctuation; protecting hyphens inside numeric ranges needs a more specific regular expression.

Common problems and next steps

If apostrophes disappear, the likely cause is that the input uses a different apostrophe character than the one you excluded. Diagnose it by printing the Unicode code point of the character with String.codePointAt after finding its index, or by running the text through a small loop that prints each non-ASCII character as an escaped value.

When periods or commas remain, the text may contain lookalike punctuation such as a fullwidth comma or a bullet-like symbol that is not in the punctuation category. In that case, replace the category-based pattern with an explicit list of accepted characters, such as letters, digits, spaces, and the apostrophes you want to keep.

For audio or video metadata, apply the same cleaning step before writing titles, descriptions, tags, or searchable fields. Keep a copy of the original string so you can compare the cleaned value and restore the source if a reviewer wants the exact original caption or file name.

If the output must preserve pronunciation hints or word timing, do not change punctuation inside timing formats such as timestamps, speaker labels, or cue identifiers. Clean only the display text field and leave structural punctuation untouched.