正则表达式和数据库读/写中的 Unicode

发布于 2024-10-04 05:40:40 字数 359 浏览 1 评论 0原文

晚安,

我目前正在基于正则表达式匹配的 C# 中为人类语言开发一个非常简单的词法分析器,我目前面临的问题是指定一个可以匹配目标语言中所有可能的标点符号的正则表达式,以及另一个可以匹配的正则表达式。还可以匹配各种空格。在 Stack Overflow 和另一个网站中搜索后,我发现我可以分别使用正则表达式 \p{P} 表示标点符号,使用 \p{Z} 表示空格,并且它可以工作从文本文件读取时。问题是,在最终的程序中,需要从 SQL 数据库读取记录并使用词法分析器处理它们,而且我不知道数据库中记录的编码是什么。在这种情况下这会成为问题吗?无论编码是什么,上述正则表达式是否可以匹配输入的每个标点符号和空白字符?

非常感谢。

Good night,

I am currently working on a very simple lexical analiser for human language in C# based on Regex matching, and I am currently facing the problem of specifing a Regex that can match every possible punctuation symbol in the target language, and another one that can match also every kind of blank spaces. After searching here in Stack Overflow and another site I found that I can respectively use Regexs \p{P} for punctuation and \p{Z} for whitespace, and it works when reading from a text file. The question is that in the final program it will be needed to read records from an SQL database and process them using this lexical analiser, and I don't know what is the encoding of the records in the DB. Can this be a problem in this situation? Can the mentioned Regexes match every punctuation and whitespace character of an input no matter what their encoding is?

Thank you very much.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一片旧的回忆 2024-10-11 05:40:40

.NET Framework 会将数据库中的字符串转换为 Unicode。是否正确转换它们取决于是否有东西告诉它数据库文本编码是什么。但您的 Regex 看到的字符串将采用 Unicode。

因此,假设数据库访问层正确转换了数据库记录中的文本,您不必担心编码,因为它始终是 Unicode。

The .NET Framework will convert strings to Unicode from the database. Whether it converts them correctly depends on whether something told it what the database text encoding was. But the strings your Regex sees will be in Unicode.

So, assuming that the database access layer correctly converted the text from the database record, you don't have to worry about encoding because it's always Unicode.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文