Web/RegExTagParser.cs

(开头部分) 3KB

这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text.RegularExpressions;
using System.Text;

using ComLib;

namespace ComLib.Web
{
    /// <summary>
    /// Parse to parse html/xml like tags in text.
    /// CAUTION!!! 
    /// This is a non-strict html light-weight implementation, instead of having a full lexical parser for html.
    /// By "non-strict" it does NOT take into account whether the parsed tags are nested inside an html comment or xml cdata
    /// <widget id="2" />
    /// </summary>
    public class RegExTagParser
    {
        private string _text;
        private string _tagName;
        private List<Tag> _tags;
        

        /// <summary>
        /// Parser for the tags.
        /// </summary>
        /// <param name="tagname">Name of tag.</param>
        public RegExTagParser(string tagname)
        {
            _tagName = tagname;
        }


        /// <summary>
        /// Parses the text and obtains a collection of the tags to parse.
        /// </summary>
        /// <param name="text">List of parsed tags.</param>
        public IList<Tag> Parse(string text)
        {
            _text = text;
            _tags = new List<Tag>();
            // \<component\s{1}\s*(?<name>[\w]+)=\"(?<val>[\w]+)\"\s*/\>
            string pattern = "\\<" + _tagName + "\\s{1}\\s*(?<name>[\\w]+)=\\\"(?<val>[\\w]+)\\\"\\s*/\\>";
            var matches = Regex.Matches(_text, pattern, RegexOptions.IgnoreCase);

            if (matches != null && matches.Count > 0)
            {
                foreach (Match match in matches)
                {
                    Tag tag = new Tag();
                    tag.Attributes = new OrderedDictionary();
                    tag.Name = _tagName;
                    tag.Position = match.Index;
                    tag.Length = match.Length;
                    Group name = match.Groups["name"];
                    if (name != null)
                    {   
                        Group val = match.Groups["val"];
后面还有 49 行代码,解锁后查看完整代码

24 小时内免费解锁 3 个项目,之后 1 积分/个。 规则说明

AI 解读

登录后可用,每次 10 积分,解读结果公开显示在下面。

还没有人解读过这个文件。