.net - Capture a part of a string that does not match another group (C# Regex) -
I am working on a project for which "formatting tags" parsing is required. Using a tag like this: & lt; B & gt; Text & lt; / B & gt;
, it modifies the way the text is displayed (that tag makes the text bold). You can have 4 identifiers ( b
bold, i
for italic, u
, and s < / Code> for Strikeout).
For example:
To parse these tags, I'm trying to capture a text before using a regx, capture the first opening tag, and then any tag and its attached text Right now, I have this: This corresponds to a tag, its attached text and a similar completion tag. Right now, I repeat through every single character and try to match the position in the string, like at the end of the string, for example i However, this approach is incredibly inefficient It seems that it would be better to match in a whole code instead of using the string manually. My actual question is that it is possible to match a string that does not match a group, like a tag? I've googled it without success, but maybe I have not used the right words. & lt; Bi & gt; Some & lt; / B & gt; Text & lt; / I & gt; Here some text will be generated.
& lt; (? & Lt; open & gt; [bius] {1,4}) (? & Lt; text & gt; +?) & Lt; / (? & Lt; close & gt; [bias] {1,4})
i = 0
But I try to match the whole string, from a 1 to the end, a substrings on i = 1
, etc.
I think that trying to parse and validate the entire text in a regular expression will give you problems The text you are parsing is not a regular language, so regular expressions are not well prepared for this purpose.
Instead, I will suggest that you will mark a single tag and text for input before input for the tag. You can use a simple regular expression to find a single tag - this is a very simple problem that can handle regular expressions very well once you have tokne it once, then you can with a normal loop Iterate over the token and can apply formatting to the text in the appropriate form.
Comments
Post a Comment