混合 Unicode 和 8 字节字符码混合 Unicode 和 8 字节字符码混合 Unicode 和 8 字节字符码混合 Unicode 和 8 字节字符码
  • 文章
  • 正则表达式
    • 工具
  • 登录
找到的结果: {phrase} (显示: {results_count} 共: {results_count_total})
显示: {results_count} 共: {results_count_total}

加载更多搜索结果...

搜索范围
模糊匹配
搜索标题
搜索内容
发表 admin at 2024年3月5日
类别
  • 正则表达式
标签
混合 Unicode 和 8 字节字符码
  • 简
  • 繁
  • En
关于正则表达式 » 正则表达式范例 » 混合 Unicode 和 8 字节字符码

范例
正则表达式范例
数字范围
浮点数
电子邮件地址
IP 地址
有效日期
数字日期转换为文本
信用卡号码
比对完整行
删除重复行
编程
两个相近的字词
陷阱
灾难性回溯
重复次数过多
拒绝服务
让所有内容变成可选
重复捕获组
混合 Unicode 和 8 字节
更多内容
简介
正则表达式快速开始
正则表达式教学
替换字符串教程
应用程序和语言
正则表达式范例
正则表达式参考
替换字符串参考

混合 Unicode 和 8 字节字符码

电脑在内部处理的是数字,而不是字符。当您保存文本档时,每个字符都会对应到一个数字,而这些数字会保存在磁盘中。当您打开文本档时,这些数字会被读取并对应回字符。在使用正则表达式处理文本时,正则表达式需要使用与您用来创建您要让正则表达式处理的文件或字符串相同的对应方式。

当您在正则表达式中输入所有字符时,您通常不需要担心任何事。提供正则表达式功能的应用程序或编程函数库会知道您的主旨字符串使用哪些文本编码,并据此进行处理。因此,如果您想要搜索欧元货币符号,而且您有欧洲键盘,只要按下 AltGr+E 即可。您的正则表达式 € 会顺利找到所有欧元符号。

但是,您无法在美国键盘上按下 AltGr+E。或者您可能希望您的原代码保持 7 字节干净(即纯 ASCII)。在这些情况下,您需要在正则表达式中使用字符转义。

如果您的正则表达式引擎支持 Unicode,请直接使用 Unicode 转义 \u20AC(大多数 Unicode 风格)或 \x{20AC}(Perl 和 PCRE)。U+20AC 是欧元符号的 Unicode 码点。它将永远比对到欧元符号,不论您的主旨字符串是使用 UTF-8、UTF-16、UCS-2 或其他任何编码。即使您的主旨字符串是使用旧版 8 字节编码页编码,也不会造成混淆。您可能需要告诉应用程序或正则表达式引擎您的文件使用哪种编码。但是 \u20AC 永远都是欧元符号。

大多数 Unicode regex 引擎也支持 8 比特字符转义 \xFF。然而,不建议使用。对于字符 \x00 到 \x7F,通常没有问题。前 128 个 Unicode 码点与大多数 8 比特码页所根据的 ASCII 表相同。

但是,\x80 以上的诠释可能有所不同。纯粹的 Unicode 引擎会将其视为与 \u0080 相同,表示拉丁语 1 控制码。但大多数人预期 \x80 会比对欧元符号,因为在所有 Windows 码页中,它占据 80h 位置。如果您的文本档使用 Windows 码页编码,则使用 8 比特 regex 引擎时会比对。

由于大多数人预期 \x80 会被视为 8 比特字符,而不是 Unicode 码点 \u0080,因此有些 Unicode regex 引擎会完全照做。有些硬件连接使用特定码页,例如 Windows 1252 或电脑的缺省码页,来诠释 8 比特字符码。

其他引擎会让它取决于输入字符串。

如果您发现上述内容令人困惑,请勿在支持 Unicode 的 regex 引擎中使用 \x80 到 \xFF。

8 比特 Regex 引擎

在使用仅处理 8 比特数据的旧式(过时?)正则表达式引擎时,您无法使用 Unicode 转义,例如 \u20AC。\x80 是您拥有的全部。请注意,即使是现代引擎也有旧式模式。例如,流行的 regex 函数库 PCRE 缺省会以 8 比特引擎运行。如果您要使用 Unicode 功能,需要明确激活 UTF-8 支持。当您运行此操作时,PCRE 也会要求您将主旨字符串转换为 UTF-8。

在为 8 比特引擎创建正则表达式时,您必须考量要使用的字符集或编码页。8 比特正则表达式引擎并不在乎。如果您在正则表达式中输入 \x80,它将比对任何字节 80h,不论该字节代表什么。在 Windows 1252 文本档中,它将是欧元符号,在 Latin-1 文件中是控制码,在 EBCDIC 文件中是数字零。

即使是在正则表达式中的字面字符,您也必须将正则表达式中使用的编码与主体编码配对。如果您的应用程序使用 Latin-1 编码页,而您使用正则表达式 À,当您搜索 Latin-2 文本档时,它将比对 Ŕ。应用程序会适当地在屏幕上显示为 À,因为它使用错误的编码页。这个问题并非正则表达式所特有。当您使用不同 8 比特编码的文件和应用程序时,您将会遇到它。

因此,在使用 8 比特数据时,请在十六进位编辑器中打开您正在使用的实际数据。查看正在使用的字节,并在您的正则表达式中指定它们。

如果使用 8 比特引擎处理 Unicode 文件,情况会变得非常棘手。让我们回到我们只有一个欧元符号的文本档。当保存为 little endian UTF-16(在 Windows 上称为「Unicode」)时,8 比特正则表达式引擎将看到两个字节 AC 20(请记住,little endian 会反转字节)。当保存为 UTF-8(没有 endianness)时,我们的 8 比特引擎将看到三个字节 E2 82 AC。您需要 \xE2\x82\xAC 来比对 8 比特正则表达式引擎中的 UTF-8 文件中的欧元符号。

混合 Unicode 和 8 位元組字元碼
  • 简
  • 繁
  • En
關於正規表示式 » 正規表示式範例 » 混合 Unicode 和 8 位元組字元碼

範例
正規表示法範例
數字範圍
浮點數
電子郵件地址
IP 位址
有效日期
數字日期轉換為文字
信用卡號碼
比對完整行
刪除重複行
程式設計
兩個相近的字詞
陷阱
災難性回溯
重複次數過多
拒絕服務
讓所有內容變成可選
重複擷取群組
混合 Unicode 和 8 位元組
本網站的更多資訊
簡介
正規表示法快速開始
正規表示法教學
替換字串教學
應用程式和語言
正規表示法範例
正規表示法參考
替換字串參考

混合 Unicode 和 8 位元組字元碼

電腦在內部處理的是數字,而不是字元。當您儲存文字檔時,每個字元都會對應到一個數字,而這些數字會儲存在磁碟中。當您開啟文字檔時,這些數字會被讀取並對應回字元。在使用正規表示法處理文字時,正規表示法需要使用與您用來建立您要讓正規表示法處理的檔案或字串相同的對應方式。

當您在正規表示法中輸入所有字元時,您通常不需要擔心任何事。提供正規表示法功能的應用程式或程式設計函式庫會知道您的主旨字串使用哪些文字編碼,並據此進行處理。因此,如果您想要搜尋歐元貨幣符號,而且您有歐洲鍵盤,只要按下 AltGr+E 即可。您的正規表示法 € 會順利找到所有歐元符號。

但是,您無法在美國鍵盤上按下 AltGr+E。或者您可能希望您的原始碼保持 7 位元組乾淨(即純 ASCII)。在這些情況下,您需要在正規表示法中使用字元跳脫。

如果您的正規表示法引擎支援 Unicode,請直接使用 Unicode 跳脫 \u20AC(大多數 Unicode 風格)或 \x{20AC}(Perl 和 PCRE)。U+20AC 是歐元符號的 Unicode 碼點。它將永遠比對到歐元符號,不論您的主旨字串是使用 UTF-8、UTF-16、UCS-2 或其他任何編碼。即使您的主旨字串是使用舊版 8 位元組編碼頁編碼,也不會造成混淆。您可能需要告訴應用程式或正規表示法引擎您的檔案使用哪種編碼。但是 \u20AC 永遠都是歐元符號。

大多數 Unicode regex 引擎也支援 8 位元字元跳脫 \xFF。然而,不建議使用。對於字元 \x00 到 \x7F,通常沒有問題。前 128 個 Unicode 碼點與大多數 8 位元碼頁所根據的 ASCII 表相同。

但是,\x80 以上的詮釋可能有所不同。純粹的 Unicode 引擎會將其視為與 \u0080 相同,表示拉丁語 1 控制碼。但大多數人預期 \x80 會比對歐元符號,因為在所有 Windows 碼頁中,它佔據 80h 位置。如果您的文字檔使用 Windows 碼頁編碼,則使用 8 位元 regex 引擎時會比對。

由於大多數人預期 \x80 會被視為 8 位元字元,而不是 Unicode 碼點 \u0080,因此有些 Unicode regex 引擎會完全照做。有些硬體連線使用特定碼頁,例如 Windows 1252 或電腦的預設碼頁,來詮釋 8 位元字元碼。

其他引擎會讓它取決於輸入字串。

如果您發現上述內容令人困惑,請勿在支援 Unicode 的 regex 引擎中使用 \x80 到 \xFF。

8 位元 Regex 引擎

在使用僅處理 8 位元資料的舊式(過時?)正規表示式引擎時,您無法使用 Unicode 跳脫,例如 \u20AC。\x80 是您擁有的全部。請注意,即使是現代引擎也有舊式模式。例如,流行的 regex 函式庫 PCRE 預設會以 8 位元引擎執行。如果您要使用 Unicode 功能,需要明確啟用 UTF-8 支援。當您執行此操作時,PCRE 也會要求您將主旨字串轉換為 UTF-8。

在為 8 位元引擎建立正規表示式時,您必須考量要使用的字元集或編碼頁。8 位元正規表示式引擎並不在乎。如果您在正規表示式中輸入 \x80,它將比對任何位元組 80h,不論該位元組代表什麼。在 Windows 1252 文字檔中,它將是歐元符號,在 Latin-1 檔案中是控制碼,在 EBCDIC 檔案中是數字零。

即使是在正規表示式中的字面字元,您也必須將正規表示式中使用的編碼與主體編碼配對。如果您的應用程式使用 Latin-1 編碼頁,而您使用正規表示式 À,當您搜尋 Latin-2 文字檔時,它將比對 Ŕ。應用程式會適當地在螢幕上顯示為 À,因為它使用錯誤的編碼頁。這個問題並非正規表示式所特有。當您使用不同 8 位元編碼的檔案和應用程式時,您將會遇到它。

因此,在使用 8 位元資料時,請在十六進位編輯器中開啟您正在使用的實際資料。查看正在使用的位元組,並在您的正規表示式中指定它們。

如果使用 8 位元引擎處理 Unicode 檔案,情況會變得非常棘手。讓我們回到我們只有一個歐元符號的文字檔。當儲存為 little endian UTF-16(在 Windows 上稱為「Unicode」)時,8 位元正規表示式引擎將看到兩個位元組 AC 20(請記住,little endian 會反轉位元組)。當儲存為 UTF-8(沒有 endianness)時,我們的 8 位元引擎將看到三個位元組 E2 82 AC。您需要 \xE2\x82\xAC 來比對 8 位元正規表示式引擎中的 UTF-8 檔案中的歐元符號。

Mixing Unicode and 8-bit Character Codes
  • 简
  • 繁
  • En
About Regular Expressions » Sample Regular Expressions » Mixing Unicode and 8-bit Character Codes

Examples
Regular Expressions Examples
Numeric Ranges
Floating Point Numbers
Email Addresses
IP Addresses
Valid Dates
Numeric Dates to Text
Credit Card Numbers
Matching Complete Lines
Deleting Duplicate Lines
Programming
Two Near Words
Pitfalls
Catastrophic Backtracking
Too Many Repetitions
Denial of Service
Making Everything Optional
Repeated Capturing Group
Mixing Unicode & 8-bit
More on This Site
Introduction
Regular Expressions Quick Start
Regular Expressions Tutorial
Replacement Strings Tutorial
Applications and Languages
Regular Expressions Examples
Regular Expressions Reference
Replacement Strings Reference

Mixing Unicode and 8-bit Character Codes

Internally, computers deal with numbers, not with characters. When you save a text file, each character is mapped to a number, and the numbers are stored on disk. When you open a text file, the numbers are read and mapped back to characters. When processing text with a regular expression, the regular expression needs to use the same mapping as you used to create the file or string you want the regex to process.

When you simply type in all the characters in your regular expression, you normally don’t have anything to worry about. The application or programming library that provides the regular expression functionality will know what text encodings your subject string uses, and process it accordingly. So if you want to search for the euro currency symbol, and you have a European keyboard, just press AltGr+E. Your regex € will find all euro symbols just fine.

But you can’t press AltGr+E on a US keyboard. Or perhaps you like your source code to be 7-bit clean (i.e. plain ASCII). In those cases, you’ll need to use a character escape in your regular expression.

If your regular expression engine supports Unicode, simply use the Unicode escape \u20AC (most Unicode flavors) or \x{20AC} (Perl and PCRE). U+20AC is the Unicode code point for the euro symbol. It will always match the euro symbol, whether your subject string is encoded in UTF-8, UTF-16, UCS-2 or whatever. Even when your subject string is encoded with a legacy 8-bit code page, there’s no confusion. You may need to tell the application or regex engine what encoding your file uses. But \u20AC is always the euro symbol.

Most Unicode regex engines also support the 8-bit character escape \xFF. However, its use is not recommended. For characters \x00 through \x7F, there’s usually no trouble. The first 128 Unicode code points are identical to the ASCII table that most 8-bit code pages are based on.

But the interpretation of \x80 and above may vary. A pure Unicode engine will treat this identical to \u0080, which represents a Latin-1 control code. But what most people expect is that \x80 matches the euro symbol, as that occupies position 80h in all Windows code pages. And it will when using an 8-bit regex engine if your text file is encoded using a Windows code page.

Since most people expect \x80 to be treated as an 8-bit character rather than the Unicode code point \u0080, some Unicode regex engines do exactly that. Some are hard-wired to use a particular code page, say Windows 1252 or your computer’s default code page, to interpret 8-bit character codes.

Other engines will let it depend on the input string.

If you find the above confusing, simply don’t use \x80 through \xFF with a regex engine that supports Unicode.

8-bit Regex Engines

When working with a legacy (obsolete?) regular expression engine that works on 8-bit data only, you can’t use Unicode escapes like \u20AC. \x80 is all you have. Note that even modern engines have legacy modes. The popular regex library PCRE, for example, runs as an 8-bit engine by default. You need to explicitly enable UTF-8 support if you want to use Unicode features. When you do, PCRE also expects you to convert your subject strings to UTF-8.

When crafting a regular expression for an 8-bit engine, you’ll have to take into account which character set or code page you’ll be working with. 8-bit regex engines just don’t care. If you type \x80 into your regex, it will match any byte 80h, regardless of what that byte represents. That’ll be the euro symbol in a Windows 1252 text file, a control code in a Latin-1 file, and the digit zero in an EBCDIC file.

Even for literal characters in your regex, you’ll have to match up the encoding you’re using in the regular expression with the subject encoding. If your application is using the Latin-1 code page, and you use the regex À, it’ll match Ŕ when you search through a Latin-2 text file. The application would duly display this as À on the screen, because it’s using the wrong code page. This problem is not really specific to regular expressions. You’ll encounter it any time you’re working with files and applications that use different 8-bit encodings.

So when working with 8-bit data, open the actual data you’re working with in a hex editor. See the bytes being used, and specify those in your regular expression.

Where it gets really hairy is if you’re processing Unicode files with an 8-bit engine. Let’s go back to our text file with just a euro symbol. When saved as little endian UTF-16 (called “Unicode” on Windows), an 8-bit regex engine will see two bytes AC 20 (remember that little endian reverses the bytes). When saved as UTF-8 (which has no endianness), our 8-bit engine will see three bytes E2 82 AC. You’d need \xE2\x82\xAC to match the euro symbol in an UTF-8 file with an 8-bit regex engine.

©2015-2025 艾丽卡 support@alaica.com