Click here to Skip to main content
11,929,166 members (51,333 online)
Click here to Skip to main content
Add your own
alternative version


21 bookmarked

Using Regular Expression for Parsing

, 16 Mar 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
Derived currently supports RTF and HTML format
Screenshot - SyntaxDemo.gif


This article discusses regular expression syntax used for analysis and analysis of the results shows the text to HTML or RTF format.


Regular Expression Analysis:

public virtual bool Analyze(string ACode)
    if (FSyntaxItems.Count <= 0) return false;
    if (ACode == null) return false;
    string vCode = ACode;
    bool vFind = true;
    while (vFind && (vCode.Length > 0))
        vFind = false;
        foreach (SyntaxItem vSyntaxItem in FSyntaxItems)
            if (Regex.IsMatch(vCode, vSyntaxItem.Pattern, vSyntaxItem.Options))
                AnalyzeResluts.Add(new AnalyzeReslut(vSyntaxItem,
                    Regex.Match(vCode, vSyntaxItem.Pattern,
                vCode = Regex.Replace(vCode, vSyntaxItem.Pattern, "",
                vFind = true;
    return true;
  • SyntaxEngineClass: Class-based parsing engine SyntaxItems type property inheritance by adding items grammar analysis
  • SyntaxHighlight: Class-based highlight engine HighlightItem type property inheritance by adding items color and font style

Machine translation of text, barcode or see:

public class SyntaxItem
    private string FPattern;
    private RegexOptions FOptions; 
    private string FName; 
    private int FIndex; 

    public string Pattern { get { return FPattern; } } 
    public RegexOptions Options { get { return FOptions; } }
    public string Name { get { return FName; } }
    public int Index { get { return FIndex; } }

    public SyntaxItem(string APattern, RegexOptions AOptions,
        string AName, int AIndex)
        FPattern = APattern;
        FOptions = AOptions;
        FName = AName;
        FIndex = AIndex;

public class AnalyzeReslut
    private SyntaxItem FItem;
    private string FBlock; 

    public SyntaxItem Item { get { return FItem; } }
    public string Block { get { return FBlock; } }

    public AnalyzeReslut(SyntaxItem AItem, string ABlock)
        FItem = AItem;
        FBlock = ABlock;

Refer to the following regular expression code written in other languages:

SyntaxItems.Add(new SyntaxItem(@"^\s+", RegexOptions.None,
    "Whitespace", SyntaxItems.Count));
SyntaxItems.Add(new SyntaxItem(@"^\/\/[^\n]*[\n]?", RegexOptions.None,
    "LineComment", SyntaxItems.Count));
SyntaxItems.Add(new SyntaxItem(@"^\/\*.*?\*\/", RegexOptions.None,
    "MultiComment", SyntaxItems.Count));

Add regular expression must be by '^', not to write expression, otherwise it would match the length of the dead cycle 0.


  • 17th March, 2007: Version 1.0


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

China China

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
ArchKaine27-Jun-11 19:38
memberArchKaine27-Jun-11 19:38 
GeneralMy vote of 1 Pin
ArchKaine31-Mar-11 15:12
memberArchKaine31-Mar-11 15:12 
GeneralMultiline patterns don't work Pin
shakeupkga27-Sep-07 7:28
membershakeupkga27-Sep-07 7:28 
GeneralRe: Multiline patterns don't work Pin
wjhu11127-Sep-07 16:36
memberwjhu11127-Sep-07 16:36 
GeneralSuggestion for readability. Pin
ArchKaine20-Mar-07 19:24
memberArchKaine20-Mar-07 19:24 
You might want to go through and describe what it is that you're doing a bit better. As it stands anyone reading it will have to guess what you're doing in the code samples given.

The code does look interesting. But give us an idea of what you're trying to accomplish with it, please. Smile | :)


Some say that ignorance is bliss... Blissful, aren't they?

GeneralRe: Suggestion for readability. Pin
wjhu11121-Mar-07 1:59
memberwjhu11121-Mar-07 1:59 
GeneralRe: Suggestion for readability. Pin
ArchKaine21-Mar-07 4:59
memberArchKaine21-Mar-07 4:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.151126.1 | Last Updated 17 Mar 2007
Article Copyright 2007 by wjhu111
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid