正则表达式匹配实现

发布于:2025-06-26 ⋅ 阅读:(21) ⋅ 点赞:(0)

直接上代码

using Microsoft.AspNetCore.Mvc;
using System.Text.RegularExpressions;

namespace SaaS.OfficialWebSite.Web.Controllers
{
    public class RegController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public IActionResult TestRegex([FromBody] RegexTestRequest request)
        {
            if (request == null || string.IsNullOrEmpty(request.Pattern) || string.IsNullOrEmpty(request.Text))
            {
                return BadRequest(new { message = "正则表达式模式和测试文本不能为空" });
            }

            try
            {
                var options = RegexOptions.None;

                if (request.Options.IgnoreCase)
                    options |= RegexOptions.IgnoreCase;

                if (request.Options.Multiline)
                    options |= RegexOptions.Multiline;

                if (request.Options.Singleline)
                    options |= RegexOptions.Singleline;

                if (request.Options.ExplicitCapture)
                    options |= RegexOptions.ExplicitCapture;

                var regex = new Regex(request.Pattern, options);
                var matches = regex.Matches(request.Text);

                var response = new RegexTestResponse
                {
                    IsMatch = matches.Count > 0,
                    Matches = new List<MatchResult>()
                };

                foreach (Match match in matches)
                {
                    var matchResult = new MatchResult
                    {
                        Value = match.Value,
                        Index = match.Index,
                        Length = match.Length,
                        Groups = new List<GroupResult>()
                    };

                    // 跳过第一个组(整个匹配)
                    for (int i = 1; i < match.Groups.Count; i++)
                    {
                        var group = match.Groups[i];
                        if (group.Success)
                        {
                            matchResult.Groups.Add(new GroupResult
                            {
                                Name = regex.GroupNameFromNumber(i),
                                Value = group.Value,
                                Index = group.Index,
                                Length = group.Length
                            });
                        }
                    }

                    response.Matches.Add(matchResult);
                }

                return Ok(response);
            }
            catch (ArgumentException ex)
            {
                return BadRequest(new { message = $"无效的正则表达式: {ex.Message}" });
            }
            catch (Exception ex)
            {
                return StatusCode(500, new { message = $"发生错误: {ex.Message}" });
            }
        }

    }
    public class RegexTestRequest
    {
        public string Pattern { get; set; }
        public string Text { get; set; }
        public RegexOptionsDto Options { get; set; }
    }

    public class RegexOptionsDto
    {
        public bool IgnoreCase { get; set; }
        public bool Multiline { get; set; }
        public bool Singleline { get; set; }
        public bool ExplicitCapture { get; set; }
    }

    public class RegexTestResponse
    {
        public bool IsMatch { get; set; }
        public List<MatchResult> Matches { get; set; }
    }

    public class MatchResult
    {
        public string Value { get; set; }
        public int Index { get; set; }
        public int Length { get; set; }
        public List<GroupResult> Groups { get; set; }
    }

    public class GroupResult
    {
        public string Name { get; set; }
        public string Value { get; set; }
        public int Index { get; set; }
        public int Length { get; set; }
    }
}

运行效果:正则表达式匹配


网站公告

今日签到

点亮在社区的每一天
去签到