Click here to Skip to main content
Click here to Skip to main content
Go to top

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

Introduction

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

Background

Regular Expression Analysis:

public virtual bool Analyze(string ACode)
{
    if (FSyntaxItems.Count <= 0) return false;
    if (ACode == null) return false;
    AnalyzeResluts.Clear();
    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,
                    vSyntaxItem.Options).Value));
                vCode = Regex.Replace(vCode, vSyntaxItem.Pattern, "",
                    vSyntaxItem.Options);
                vFind = true;
                break;
            }
        }
    }
    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.

History

  • 17th March, 2007: Version 1.0

License

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

Share

About the Author

wjhu111

China China
zswang

Comments and Discussions

 
GeneralMy vote of 5 PinmemberArchKaine27-Jun-11 18:38 
GeneralMy vote of 1 PinmemberArchKaine31-Mar-11 14:12 
GeneralMultiline patterns don't work Pinmembershakeupkga27-Sep-07 6:28 
GeneralRe: Multiline patterns don't work Pinmemberwjhu11127-Sep-07 15:36 
GeneralSuggestion for readability. PinmemberArchKaine20-Mar-07 18:24 
GeneralRe: Suggestion for readability. Pinmemberwjhu11121-Mar-07 0:59 
Thank you for your suggestion, limited English proficiency
 
A person's life is limited, the creativity is unlimited

GeneralRe: Suggestion for readability. PinmemberArchKaine21-Mar-07 3:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web02 | 2.8.140916.1 | Last Updated 17 Mar 2007
Article Copyright 2007 by wjhu111
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid