Click here to Skip to main content
13,199,669 members (62,753 online)
Click here to Skip to main content
Add your own
alternative version


21 bookmarked
Posted 16 Mar 2007

Using Regular Expression for Parsing

, 16 Mar 2007
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 18:38
memberArchKaine27-Jun-11 18:38 
GeneralMy vote of 1 Pin
ArchKaine31-Mar-11 14:12
memberArchKaine31-Mar-11 14:12 
Uncommented code. Poor response to constructive criticism.
GeneralMultiline patterns don't work Pin
shakeupkga27-Sep-07 6:28
membershakeupkga27-Sep-07 6:28 
GeneralRe: Multiline patterns don't work Pin
wjhu11127-Sep-07 15:36
memberwjhu11127-Sep-07 15:36 
GeneralSuggestion for readability. Pin
ArchKaine20-Mar-07 18:24
memberArchKaine20-Mar-07 18:24 
GeneralRe: Suggestion for readability. Pin
wjhu11121-Mar-07 0:59
memberwjhu11121-Mar-07 0:59 
GeneralRe: Suggestion for readability. Pin
ArchKaine21-Mar-07 3:59
memberArchKaine21-Mar-07 3: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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171020.1 | Last Updated 17 Mar 2007
Article Copyright 2007 by wjhu111
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid