Click here to Skip to main content
15,885,546 members
Articles / Programming Languages / C#
Article

Using Regular Expression for Parsing

Rate me:
Please Sign up or sign in to vote.
2.85/5 (4 votes)
16 Mar 2007CPOL 36.5K   264   21   7
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:

C#
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:

C#
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)


Written By
China China
zswang

Comments and Discussions

 
GeneralMy vote of 5 Pin
ArchKaine27-Jun-11 18:38
ArchKaine27-Jun-11 18:38 
GeneralMy vote of 1 Pin
ArchKaine31-Mar-11 14:12
ArchKaine31-Mar-11 14:12 
GeneralMultiline patterns don't work Pin
shakeupkga27-Sep-07 6:28
shakeupkga27-Sep-07 6:28 
GeneralRe: Multiline patterns don't work Pin
wjhu11127-Sep-07 15:36
wjhu11127-Sep-07 15:36 
GeneralSuggestion for readability. Pin
ArchKaine20-Mar-07 18:24
ArchKaine20-Mar-07 18:24 
GeneralRe: Suggestion for readability. Pin
wjhu11121-Mar-07 0:59
wjhu11121-Mar-07 0:59 
GeneralRe: Suggestion for readability. Pin
ArchKaine21-Mar-07 3:59
ArchKaine21-Mar-07 3:59 
wjhu111 wrote:
Thank you for your suggestion, limited English proficiency


Yeah, I figured that was the case, but if you take your time, you can do a pretty good job of explaining things better, and learn a bit more English in the process. Overall a good potential learning experience. Have fun with it Smile | :)

One good place to start, if you don't mind the suggestion, is converting the comments in your code to English. That would greatly help coders to understand what you're doing in the code.

If you need help in doing that, feel free to drop me a note, and I'll see what I can do.

Happy coding! Smile | :)
ArchKaine

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

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.