Cleaning Up Text You Pasted From Somewhere Else

Invisible spaces, broken PDF line breaks, stray capitals and duplicate lines: the order to fix pasted text in, with a measured four-tool walkthrough.

Text that arrives through the clipboard brings its history with it: the line breaks of a PDF page, the quote markers of an email reply, the no-break spaces of a web page that look like ordinary spaces and behave differently. None of it is visible at a glance, and most of it is fixed in a few minutes once you know which tool handles which kind of mess. This guide goes through the usual problems in the order they tend to come up, and finishes with a measured example of one paragraph taken from a PDF to a clean line of prose.

First, look at what you actually pasted

Before changing anything, paste the text into the Whitespace Visualizer, which marks each ordinary space with a middle dot, a tab with an arrow, a no-break space with ⍽ and each line ending with ↵. Here is a quarterly-report paragraph copied from a PDF, as the visualizer showed it:

→···The··regional·sales·team·closed·148·new·accounts·in·the·third↵
quarter,·up·from·131·in·the·second.··Average·contract·value·rose↵
to·€4,250·(Q2:·€3,980),·driven·mainly·by·the·Enterprise·tier.↵
↵
Page·3·of·12↵
↵
Renewal·rate·held·at·91⍽%,·and·churn·in·the·SMB·segment·fell·to↵
2.4⍽%·after·the·onboarding·changes·introduced·in·June.↵

Four separate problems are now visible: a tab at the start, double spaces after full stops, a page footer with blank lines around it, and two no-break spaces between the numbers and their percent signs. Each needs a different tool, and the order matters.

Extra spaces, tabs and blank lines

The Text Cleaner has three checkboxes. Remove extra spaces collapses any run of spaces or tabs inside a line to a single space and trims both ends of every line. Remove empty lines drops lines that are empty or contain only whitespace. Remove duplicate lines drops a line if an identical line, compared after trimming, appeared earlier, keeping the first copy. When more than one box is ticked they apply in that order.

Two things it does not do. It leaves line breaks where they are, so a hard-wrapped PDF paragraph stays hard-wrapped. And the collapsing only covers ordinary spaces and tabs: a no-break space (U+00A0) inside a line, the kind word processors insert between a number and its unit, passes straight through; in the measured run the two ⍽ marks survived the cleaner untouched.

No-break spaces and other characters the cleaner ignores

For characters the cleaner skips, use Find & Replace. It treats the search text literally, so pasting a single no-break space into the Find box and a normal space into the Replace box swaps every one of them. The same approach works for curly quotes, soft hyphens and zero-width characters: copy the odd character out of the pasted text into the Find box.

By default the search ignores case, which matters more than it sounds. Searching for acme ltd in a sentence that contained both “Acme Ltd” and “ACME Ltd” replaced both with “Acme Limited”; ticking the case-sensitive box and running the same search changed nothing, because neither spelling matched the lower-case search text. Punctuation is literal too: searching for a full stop replaced the full stops and nothing else, it did not act as a wildcard.

Line breaks from PDFs and emails

PDF text is laid out line by line, so copying it gives a line break wherever the page had one; email clients wrap at about seventy characters and prefix quoted lines with >. The Join Lines tool reverses this: set the separator to Space, tick Trim each line and Remove empty lines, and every line in the box becomes one line of output.

It joins everything in the box, so work one paragraph at a time. With Remove empty lines unticked, each blank line becomes an extra space: the measured run produced a double space where the footer had been. For email quotes, strip the > prefix with Find & Replace first.

Walkthrough: one PDF paragraph through four tools

Here is the paragraph from the visualizer, taken to a single clean line. Every count comes from the Word Counter and every intermediate result was copied from the tool that produced it.

  1. Starting point. The pasted text measures 59 words, 324 characters, 260 without spaces and 3 paragraphs, the blank lines around the footer making three blocks.
  2. Text Cleaner, with the first two boxes ticked. Tab, double spaces and blank lines are gone, so the footer now sits as a line in the middle of the paragraph. Count: 59 words, 318 characters, 1 paragraph. The two no-break spaces are still there.
  3. Find & Replace, twice. First, Find Page 3 of 12 with an empty Replace box, which leaves an empty line where the footer was; then Find a pasted no-break space and Replace with a normal space. Checked in the visualizer: zero ⍽ marks.
  4. Join Lines, separator Space, trim on, remove empty lines on. Output:
The regional sales team closed 148 new accounts in the third quarter, up from 131 in the second. Average contract value rose to €4,250 (Q2: €3,980), driven mainly by the Enterprise tier. Renewal rate held at 91 %, and churn in the SMB segment fell to 2.4 % after the onboarding changes introduced in June.

Final count: 55 words, 305 characters, 251 without spaces, 1 paragraph. The four words that disappeared are the footer. The counter already treated “91 %” as two words before the no-break space was replaced, because it splits on any Unicode whitespace; the replacement matters for how the text behaves in a document, not for the count.

Inconsistent capitalisation

The Text Case Converter has four buttons. UPPERCASE and lowercase do what they say; the other two behave in ways worth knowing before you use them on a heading. Given quarterly review – Q3 2026: enterprise tier and SMB churn. O'Brien's PDF export is attached.:

Title Case gave Quarterly Review – Q3 2026: Enterprise Tier And Smb Churn. O'Brien'S Pdf Export Is Attached. and Sentence case gave Quarterly review – q3 2026: enterprise tier and smb churn. o'brien's pdf export is attached.

Title Case lowercases everything first and then capitalises the first letter of every word, so acronyms become “Smb” and “Pdf”, and the letter after an apostrophe counts as a new word, giving “O'Brien'S”. Sentence case lowercases everything and capitalises only the first character of the whole text, not the first character of each sentence. Both need a read-through afterwards if the text contains acronyms, names or more than one sentence.

Duplicate lines and sorting

The third box in the Text Cleaner removes repeated lines, compared after trimming but with case intact. In seven lines where “Lee, Ann” appeared twice (once with a trailing space), “Ortiz, Ben” twice and “lee, Ann” once, the output kept four: the trailing-space copy counted as a duplicate, the lower-case one did not.

The Line Sorter orders lines A to Z, Z to A, numerically or by length, with options to ignore letter case and to trim first. With case ignored, “Lee, Ann” and “lee, Ann” sit together in their original order; without it, the lower-case entry sorts first. Numeric mode matters for figures: 12, 9, 148, 2.4, Total, 31 sorted A to Z came out as 12, 148, 2.4, 31, 9, Total, compared as text; in Numeric mode as 2.4, 9, 12, 31, 148, Total, with the non-numeric line last.

Lists that need bullets or numbers

The List Formatter takes one item per line, trims each, drops empty lines and writes the list in one of five shapes: for four meeting-preparation tasks the bullet output began • Confirm venue booking, the numbered output 1. Confirm venue booking, Comma separated put , between items on one line, JSON array gave a quoted array, and Quoted CSV values joined the items with commas, quoting only an item that itself contains one.

When the numbering has to start somewhere other than 1, or use a different separator, the Line Number Tool does that: starting number 4 with separator ) produced 4) Confirm venue booking through 7) Book projector. Its Remove numbers button strips existing numbering: 1. , 2) , 3 - and 4: were all removed, but 5 Call the caterer was left alone, because a number followed directly by text is not numbering.

Find-and-replace versus a regular expression

Find & Replace is right when you know the exact text and wrong when it varies: the footers of a twelve-page PDF read “Page 3 of 12”, “Page 4 of 12” and so on, and a literal search catches one at a time. The Regex Tester shows what a pattern would match before you commit to it: Page \d+ of \d+ run against two pasted pages reported 2 matches with their character positions, and INV-(\d{4}) against a note mentioning INV-2041, INV-2042 and inv-2043 found 2 matches with the g flag and 3 with gi, reporting the captured four-digit number for each.

The tester finds; it does not replace. Use it to confirm the pattern and see which strings are involved, then do the replacements with Find & Replace, once per distinct string, or delete the listed lines by hand when there are few. The flags field always adds g, so every match is listed, not just the first.

Counting words for a limit

The Word Counter reports words, characters, characters without spaces and paragraphs as you type. A word is any run of non-space characters, so €4,250 is one word and a hyphenated term is one word. Paragraphs are blocks separated by a blank line, which is why the PDF text counted as three paragraphs until the blank lines around the footer went. Forms that count differently usually count characters, and both character figures are shown.

Markdown: tables and a preview

The Markdown Table Generator accepts tab-, semicolon- or comma-separated lines, detecting the delimiter in that order, and writes a pipe table with the first line as the header. A pipe inside a cell is escaped as \|, the alignment selector rewrites the separator row (Right gives | ---: | ---: | ---: |), and a quoted CSV cell such as "Paper, A4 (box)" stays one cell.

The Markdown Preview renders as you type and exports the result as markdown-export.html. It covers headings, bold, italic, inline code, links, block quotes, bullet lists, numbered lists, fenced code blocks and pipe tables: in the measured run a three-line table with ---: under the second header rendered as a table with that column right-aligned, and the 1. and 2. items became a numbered list. It does not handle footnotes, task-list check boxes or HTML embedded in the Markdown, which is shown as text, so check those in the system the text is destined for.

The order that avoids rework

  1. Look first, in the Whitespace Visualizer.
  2. Collapse spaces and drop blank lines with the Text Cleaner.
  3. Replace what the cleaner ignores and delete footers and quote prefixes with Find & Replace, guided by the Regex Tester when the text varies.
  4. Rejoin hard-wrapped lines, one paragraph at a time, with Join Lines.
  5. Only then fix case, sort, de-duplicate or number the lines, and count against the limit.

Doing case conversion or sorting before the whitespace is clean produces results that look right and are not: a trailing space makes two lines “different” to the duplicate check, and a tab at the start of a line changes where it sorts.

看不見的空白、PDF 斷行、大小寫混亂與重複行:清理貼上文字的處理順序,附四個工具實測的完整示範。

經由剪貼簿而來的文字,會把它的來歷一起帶過來。從 PDF 複製的段落保留著版面上的換行;回覆的電子郵件帶著引用符號與簽名檔;從網頁貼過來的表格夾著不換行空格,看起來與一般空格無異,行為卻不同。這些一眼都看不出來,但只要知道哪種亂象該交給哪個工具,多數幾分鐘內就能處理完。本文依照問題通常出現的順序逐一說明,使用的都是本站的文字工具,最後以一段從 PDF 貼出的文字為例,記錄它從原始貼上到變成一行乾淨文字的實測過程。

先看清楚你到底貼了什麼

在動手修改之前,先把文字貼進空白字元視覺化。它會把每個一般空格標成中點、Tab 標成箭頭、不換行空格標成開口方框 ⍽,每個行尾則標成 ↵。下面是一段從 PDF 複製的季報段落,與視覺化工具顯示的內容完全一致:

→···The··regional·sales·team·closed·148·new·accounts·in·the·third↵
quarter,·up·from·131·in·the·second.··Average·contract·value·rose↵
to·€4,250·(Q2:·€3,980),·driven·mainly·by·the·Enterprise·tier.↵
↵
Page·3·of·12↵
↵
Renewal·rate·held·at·91⍽%,·and·churn·in·the·SMB·segment·fell·to↵
2.4⍽%·after·the·onboarding·changes·introduced·in·June.↵

四個互不相關的問題現在都看得見了:開頭有一個 Tab、句點後有連續兩個空格、中間夾著一行頁尾且前後各有一個空行、數字與百分號之間有兩個不換行空格。每一種都需要不同的工具,而且處理順序有影響,所以下面的實作會一項一項來。

多餘的空格、Tab 與空行

文字清理器有三個核取方塊,先弄清楚每一個確切做什麼會很有幫助。「移除多餘空格」會把一行之內連續的空格或 Tab 壓成一個空格,並去掉每一行頭尾的空白;「移除空行」會丟掉空的或只含空白的行;「移除重複行」會在某一行(去除頭尾空白後比較)與前面某一行完全相同時把它丟掉,只保留第一次出現的那一份。勾選多個時,依上述順序套用。

有兩件事它不會做。它不會動換行,所以 PDF 裡被硬性折行的段落處理完仍是折行的。而且壓縮的只有一般空格與 Tab:行內的不換行空格(Unicode U+00A0,文書軟體與網頁常把它放在數字與單位之間)會原樣通過。在下面的實測中,兩個 ⍽ 記號經過清理器後原封不動。

不換行空格與其他清理器略過的字元

清理器略過的字元,交給尋找與取代處理。它把搜尋文字當作字面內容,所以把一個不換行空格貼進「尋找」欄、一個普通空格貼進「取代為」欄,就能把它們全部換掉。彎引號、軟連字號,以及有時從聊天軟體帶來的零寬字元,都可以用同樣的方法:從貼上的文字裡把那個怪字元複製到「尋找」欄即可。

預設搜尋不分大小寫,這一點比聽起來更重要。在同時含有「Acme Ltd」與「ACME Ltd」的句子裡搜尋 acme ltd,兩者都被換成了「Acme Limited」;勾選區分大小寫再執行同一次搜尋,結果完全沒變,因為兩種寫法都與小寫的搜尋文字不符。標點也是字面比對:搜尋句點只會取代句點,不會變成萬用字元。

來自 PDF 與電子郵件的換行

PDF 的文字是逐行排版的,所以複製出來時,頁面上每個折行處都會變成一個換行。郵件軟體則大約在七十個字元處折行,並在引用的行前面加上 >。合併行工具可以把這些還原:分隔符號選「空格」,勾選「去除每行頭尾空白」與「移除空行」,方塊裡的每一行就會合併成一行輸出。

它會把方塊裡的所有內容合併成一行,所以文字若有多個段落,請一次處理一段。若沒有勾選「移除空行」,每個空行都會在輸出中變成一個多餘的空格:實測中,頁尾被移除的位置就出現了連續兩個空格。至於郵件引文,先用尋找與取代去掉 > 前綴,再合併。

實作:一段 PDF 文字經過四個工具

以下是視覺化工具裡那段文字,從貼上的原始狀態一路變成一行乾淨文字的過程。每個數字都來自字數統計,每個中間結果都是直接從產生它的工具複製出來的。

  1. 起點。貼上的文字量測為 59 個字、324 個字元、不含空白 260 個字元、3 個段落:頁尾前後的空行讓計數器看到三個區塊。
  2. 文字清理器,勾選前兩個方塊。開頭的 Tab 與連續空格都不見了,空行也被丟掉,於是頁尾現在成了段落中間的一行。統計:59 個字、318 個字元、1 個段落。兩個不換行空格還在。
  3. 尋找與取代,執行兩次。第一次「尋找」Page 3 of 12、「取代為」留空,頁尾的位置會留下一個空行。第二次「尋找」貼入一個不換行空格、「取代為」一個普通空格。回到視覺化工具檢查:⍽ 記號為零。
  4. 合併行,分隔符號「空格」、去除頭尾空白開啟、移除空行開啟。輸出:
The regional sales team closed 148 new accounts in the third quarter, up from 131 in the second. Average contract value rose to €4,250 (Q2: €3,980), driven mainly by the Enterprise tier. Renewal rate held at 91 %, and churn in the SMB segment fell to 2.4 % after the onboarding changes introduced in June.

最終統計:55 個字、305 個字元、不含空白 251 個字元、1 個段落。少掉的四個字就是那行頁尾。要注意的是,在不換行空格被取代之前,計數器就已經把「91 %」當成兩個字了,因為它以任何 Unicode 空白字元切分;這次取代影響的是文字貼進文件之後的行為,而不是字數。

大小寫不一致

文字大小寫轉換有四個按鈕。UPPERCASE 與 lowercase 就如字面所示;另外兩個的行為,值得在拿它處理標題之前先弄清楚。輸入 quarterly review – Q3 2026: enterprise tier and SMB churn. O'Brien's PDF export is attached. 之後:

Title Case 得到 Quarterly Review – Q3 2026: Enterprise Tier And Smb Churn. O'Brien'S Pdf Export Is Attached.,Sentence case 得到 Quarterly review – q3 2026: enterprise tier and smb churn. o'brien's pdf export is attached.

Title Case 會先把全部轉成小寫,再把每個單字的第一個字母改成大寫,所以縮寫變成了「Smb」與「Pdf」,撇號後面的字母也被當成新單字的開頭,得到「O'Brien'S」。Sentence case 則是全部轉小寫後,只把整段文字的第一個字元改成大寫,而不是每個句子的第一個字元。若文字含有縮寫、人名或超過一個句子,兩者事後都需要再讀一遍。

重複行與排序

從多個來源湊起來的清單常會自己重複。文字清理器的第三個方塊會移除重複的行,比較時先去掉頭尾空白,但大小寫照舊。在一份七行的名單中,「Lee, Ann」出現兩次(其中一次結尾多了一個空格)、「Ortiz, Ben」出現兩次、小寫的「lee, Ann」出現一次,輸出保留了四行:結尾多空格的那一份被認定為重複,小寫的那一份則沒有。

文字行排序可以把行依 A 到 Z、Z 到 A、數值或長度排序,並可選擇忽略大小寫與先去除每行頭尾空白。忽略大小寫對人名很重要:開啟時「Lee, Ann」與「lee, Ann」會依原本的先後順序相鄰;關閉時小寫的那一筆會排在前面。數值模式對數字很重要:12, 9, 148, 2.4, Total, 31 這幾行依 A 到 Z 排序的結果是 12, 148, 2.4, 31, 9, Total,因為它們被當成文字比較;改用數值模式,結果是 2.4, 9, 12, 31, 148, Total,非數值的那一行排在最後。

需要加項目符號或編號的清單

清單格式轉換接受每行一個項目,去除每行頭尾空白並丟掉空行之後,以五種格式之一輸出清單。以四項會議準備工作為例,項目符號輸出以 • Confirm venue booking 開頭,編號輸出以 1. Confirm venue booking 開頭,逗號分隔輸出是一行、項目之間以 , 連接,JSON 陣列輸出是可直接交給程式使用、加了引號的陣列,加引號的 CSV 值則是以逗號連接各項目,只有項目本身含有逗號時才加上引號。

如果編號要從 1 以外的數字開始,或要用不同的分隔符號,行號工具可以辦到:起始編號 4、分隔符號 ) ,產生的結果從 4) Confirm venue booking 到 7) Book projector。它的「移除編號」按鈕會去掉既有的編號,實測也顯示了它的界線:1. 、2) 、 3 - 與 4: 都被移除了,但 5 Call the caterer 原樣保留,因為數字後面直接接文字不會被視為編號。

尋找取代與正規表示式的分工

知道確切文字時,尋找與取代是對的工具;文字有變化時它就不對了:一份十二頁的 PDF 裡,頁尾寫著「Page 3 of 12」、「Page 4 of 12」等等,字面搜尋一次只能抓到一個。正規表示式測試器可以在你動手之前先顯示一個模式會比對到什麼。模式 Page \d+ of \d+ 對兩頁貼上的文字執行,回報 2 個符合項目,並逐一列出它們在文字中的字元位置。模式 INV-(\d{4}) 對一段提到 INV-2041、INV-2042 與 inv-2043 的備註執行,旗標為 g 時找到 2 個、gi 時找到 3 個,而且每一個都回報了擷取到的四位數字。

測試器只負責找,不負責取代。用它確認模式、看清楚究竟牽涉到哪些字串,然後再用尋找與取代逐一處理每一種不同的字串;若只有少數幾行,直接手動刪掉列出來的那幾行也可以。旗標欄位一律補上 g,因此列出的是全部符合項目,而不是只有第一個。

為了字數上限而計算字數

字數統計會在你輸入時即時更新,回報字數、字元數、不含空白的字元數與段落數。一個「字」是任何一串連續的非空白字元,所以 €4,250 算一個字、連字號連接的詞算一個字,而數字中間若誤入一個空格就會被拆成兩個。段落是以至少一個空行隔開的區塊,這就是為什麼貼上的 PDF 文字在頁尾前後的空行被移除之前算作三個段落。如果投稿表單的算法不同,通常是算字元數,而這裡的兩個字元數字已涵蓋兩種慣例。

Markdown:表格與預覽

貼過來的表格文字常常最終要放進 wiki 頁面或工單。Markdown 表格產生器接受以 Tab、分號或逗號分隔的行,依此順序偵測分隔符號,並以第一行作為表頭寫出管線表格。儲存格內的管線符號會被跳脫成 \|,對齊選項會改寫分隔列,所以選「靠右」會得到 | ---: | ---: | ---: |。加了引號的 CSV 儲存格也能處理:實測中 "Paper, A4 (box)" 仍是一個儲存格。

Markdown 預覽器會隨輸入即時呈現,並可把結果匯出成名為 markdown-export.html 的 HTML 檔案。它支援標題、粗體、斜體、行內程式碼、連結、引用區塊、項目清單、編號清單、程式碼區塊與管線表格:實測中一個三行表格、第二欄標題下寫 ---:,呈現為表格且該欄靠右對齊,1. 與 2. 項目則成了編號清單。它不處理註腳、待辦核取方塊,也不呈現夾在 Markdown 裡的 HTML(會顯示成文字),這些請到最終要放的系統裡檢查。

避免返工的順序

  1. 先用空白字元視覺化看一遍,知道存在哪些問題。
  2. 用文字清理器壓縮空格、丟掉空行。
  3. 用尋找與取代處理清理器略過的字元,並刪除頁尾與引用前綴;文字有變化時,先用正規表示式測試器確認。
  4. 用合併行把硬性折行的行重新接起來,一次一段。
  5. 到這時才處理大小寫、排序、去重或編號,再對照字數上限計算。

在空白還沒清乾淨之前就轉換大小寫或排序,得到的結果看起來對、其實不對:結尾多一個空格會讓兩行在重複檢查裡被視為「不同」,行首多一個 Tab 則會改變它的排序位置。