Handling custom escape characters

I am working on a new feature for a C # application that processes user-defined text. This text can contain any character, but everything that is between curly brackets ({}) or between brackets ([]) will be processed in a special way (basically the text inside the brackets will be replaced for other text, and the curly brackets will display a subsection in This text will be processed differently).

So, I want to give the user the opportunity to use braces and parentheses in my text, so the first thing I thought is 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 this is the same thing that he would write if he wanted the character before this subsection was "{". Thus, this causes ambiguity.

Now I think I can use "\" to escape brackets and parentheses and use "\\" to represent "\". And I sort of figured out how to handle it, but I got the feeling that I'm trying to invent a wheel here. I wonder if there is a known algorithm or library that does what I'm trying to do.

+4
source share
3 answers

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

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

This approach allows you to parse your text using any XML parsing in Microsoft.NET and on any other platform. And you save time, because nothing can be avoided.

+1
source

Why don't you use your existing pegging agreement? There are many easy syntaxes to choose from; depending on your user population, some may already be familiar with MediaWiki and / or BBcode and / or reST and / or Markdown markups.

+2
source

I would recommend using \ to avoid {} characters in the text and un-escaped {} to surround the subsection. This is how C # handles "characters in a line". Using double curly braces leads to ambiguity and makes proper text processing difficult, if not impossible, and your choice also depends on your target users. Developers are comfortable using escape characters, but they can be confusing, -dev. You can use tags like <sub> and </sub> to specify a subkey. In any case, you can use the regular expression to parse user text in the RegEx.Matches collection.

+1
source

All Articles