处理自定义语言上的转义字符

发布于 2024-12-03 04:55:20 字数 412 浏览 4 评论 0原文

我正在为 C# 应用程序开发一项新功能,该功能将处理用户给出的文本。该文本可以包含任何字符,但大括号 ({}) 或中括号 ([]) 之间的所有内容都将以特殊方式处理(基本上,中括号内的文本将替换为另一个文本,并且大括号将指示给定文本中的一个小节,将以不同的方式处理)。

所以,我想让用户选择在他的文本上使用大括号和中括号,所以我首先想到的是使用“{{”来表示“{”,对于所有其他特殊字符也是如此,但这会提出问题。如果他想打开一个小节并希望该小节中的第一个字符是“{”,那么他会写“{{{”,但是如果他希望该小节之前的字符是“{”,那么他会写同样的东西“{”。所以这会导致歧义。

现在我想我可以使用“\”来转义大括号和方括号,并使用“\\”来表示“\”。我正在弄清楚如何处理这个问题,但我有一种感觉,我正在尝试在这里重新发明轮子。想知道是否有已知的算法或库可以完成我想要做的事情。

I'm working on a new feature for a C# application that will process a text given by the user. This text can contain any character, but everything that is between braces ({}) or between brackets ([]) will be treated on a special way (basically, the text inside brackets will be replaced for another text, and the braces will indicate a subsection in the given text and will be processed differently).

So, I want to give the user the choice to use braces and brackets on his text, so the first thing I thought was to use "{{" to represent "{", and the same for all other special characters, but this will give problems. If he wants to open a subsection and wants the first character in the subsection to be "{", then he would write "{{{", but that's the same thing he would write if he would like the character before the subsection to be "{". So this causes an ambiguity.

Now I'm thinking I could use "\" to escape braces and brackets, and use "\\" to represent "\". And I'm kinda figuring out how to process this, but I got a feeling I'm trying to reinvent the wheel here. Wonder if there is a known algorithm or library that does what I'm trying to do.

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

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

发布评论

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

评论(3

笑叹一世浮沉 2024-12-10 04:55:20

为什么不使用现有的标记约定?有很多轻量级语法可供选择;根据您的用户群体,其中一些人可能已经熟悉 MediaWiki 标记和/或 BBcode 和/或 reST 和/或 Markdown。

Why don't you use an existing markup convention? There are plenty of lightweight syntaxes to choose from; depending on your user population, some of them might already be familiar with MediaWiki markup and/or BBcode and/or reST and/or Markdown.

四叶草在未来唯美盛开 2024-12-10 04:55:20

为什么不使用 XML 标签而不是特殊字符?

<section>
Blah blah blah blah <replace id="some identifier" />
</section>

这种方法允许您使用 Microsoft .NET 和任何其他平台中的任何 XML 解析器来解析文本。而且您会节省时间,因为没有什么可以逃避的。

Why don't you use XML tags instead of special characters?

<section>
Blah blah blah blah <replace id="some identifier" />
</section>

This approach would let you parse your text using any XML parser in Microsoft .NET and any other platform. And you'll save time because there's nothing to escape.

以可爱出名 2024-12-10 04:55:20

我建议使用 \ 来转义文本中的 {} 字符,并使用未转义的 {} 来包围小节。这就是 C# 处理字符串中的 " 字符的方式。使用双括号会产生歧义,并且使正确处理文本变得困难(如果不是不可能的话)。您的选择还取决于您的目标用户。开发人员可以轻松地使用转义字符,但它们可能会让非-dev 用户。您可能想要使用 等标记来指示小节,无论哪种方式,您都可以使用正则表达式来表示。解析将用户的文本放入 RegEx.Matches 集合中。

I'd recommend using \ to escape {} chars in the text and un-escaped {} to surround a subsection. This is how C# handles " chars in a string. Using double braces introduces ambiguities and makes correctly processing the text difficult, if not impossible. Your choice also depends on your target users. Developers are comfortable using escape chars but they can be confusing to non-dev users. You might want to use tags like <sub> and </sub> to indicate a subsection. Either way, you can use a regular expression to parse the user's text into a RegEx.Matches collection.

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