C# 提取、截取或匹配字符串内包含指定字符的一些方法分享

📅 2026/7/5 15:19:30
C# 提取、截取或匹配字符串内包含指定字符的一些方法分享
目录方案一提取完整的组合字符串推荐方案二分别提取“层数”和“寝室号”的数字方案三灵活提取特定格式通用工具方法总结如果您喜欢此文章请收藏、点赞、评论谢谢祝您快乐每一天。C# 提取、截取或匹配字符串内包含指定字符如5号综合楼3层305寝2号或4号综合楼2层205寝1号。针对字符串5号综合楼3层305寝2号或4号综合楼2层205寝1号提取“层数字”如层305或“数字寝”如305寝这类组合核心在于使用‌正则表达式的捕获组‌。以下是几种针对不同需求的 C# 实现方案方案一提取完整的组合字符串推荐如果你希望直接得到层305、305寝这样的完整子串可以使用“或”逻辑的正则。using System;using System.Text.RegularExpressions;using System.Collections.Generic;public class Extractor{public static Liststring ExtractCombinations(string input){var results new Liststring();if (string.IsNullOrEmpty(input)) return results;// 正则解释// (层\d) : 匹配 层 后面跟着数字例如 层305// | : 或者// (\d寝) : 匹配 数字 后面跟着 寝例如 305寝string pattern (层\d)|(\d寝);MatchCollection matches Regex.Matches(input, pattern);foreach (Match match in matches){// match.Value 会返回整个匹配到的文本无论是 层305 还是 305寝if (match.Success){results.Add(match.Value);}}return results;}}// 调用示例class Program{static void Main(){string text 5号综合楼3层305寝2号或4号综合楼2层205寝1号;var items Extractor.ExtractCombinations(text);// 输出: 层305, 305寝, 层205, 205寝Console.WriteLine(string.Join(, , items));}}方案二分别提取“层数”和“寝室号”的数字如果你需要将“层”和“寝”分开处理或者只想要纯数字可以使用分组捕获。using System;using System.Text.RegularExpressions;public class DetailedExtractor{public class RoomInfo{public string FloorPart { get; set; } // 例如: 层305public int FloorNum { get; set; } // 例如: 305public string RoomPart { get; set; } // 例如: 305寝public int RoomNum { get; set; } // 例如: 305}public static ListRoomInfo ExtractDetails(string input){var results new ListRoomInfo();// 这里我们假设一个完整的地址结构来一次性提取这样能对应上哪层是哪个寝// 模式: ...层(数字)...(数字)寝...string pattern 层(\d).?(\d)寝;// 注意如果字符串里有多个地址上面的简单正则可能会跨地址匹配。// 更严谨的做法是分别提取或者使用更复杂的非贪婪匹配。// 为了演示简单提取所有“层X”和“Y寝”// 1. 提取所有 层数字var floorMatches Regex.Matches(input, 层(\d));// 2. 提取所有 数字寝var roomMatches Regex.Matches(input, (\d)寝);// 由于原字符串结构对称我们可以假设第i个层对应第i个寝int count Math.Min(floorMatches.Count, roomMatches.Count);for(int i0; icount; i){results.Add(new RoomInfo{FloorPart 层 floorMatches[i].Groups.Value,FloorNum int.Parse(floorMatches[i].Groups.Value),RoomPart roomMatches[i].Groups.Value 寝,RoomNum int.Parse(roomMatches[i].Groups.Value)});}return results;}}方案三灵活提取特定格式通用工具方法如果你只需要一个通用的方法传入关键字如“层”或“寝”来提取相邻数字using System;using System.Text.RegularExpressions;using System.Collections.Generic;public class FlexibleExtractor{/// summary/// 提取指定关键字前后的数字组合/// /summary/// param nameinput源字符串/param/// param namekeyword关键字如 层 或 寝/param/// param namepositionBefore表示数字在关键字前(305寝)After表示数字在关键字后(层305)/parampublic static Liststring ExtractByKeyword(string input, string keyword, string position After){var results new Liststring();if (string.IsNullOrEmpty(input)) return results;string pattern ;if (position.Equals(After, StringComparison.OrdinalIgnoreCase)){// 匹配: 关键字 数字 (例如: 层305)// \Q...\E 用于转义关键字中的特殊字符虽然中文通常不需要但这是好习惯pattern $({Regex.Escape(keyword)}\d);}else{// 匹配: 数字 关键字 (例如: 305寝)pattern $(\d{Regex.Escape(keyword)});}MatchCollection matches Regex.Matches(input, pattern);foreach (Match match in matches){results.Add(match.Groups.Value);}return results;}}// 调用示例class Program{static void Main(){string text 5号综合楼3层305寝2号或4号综合楼2层205寝1号;// 提取 层305, 层205var floors FlexibleExtractor.ExtractByKeyword(text, 层, After);Console.WriteLine(层信息: string.Join(, , floors));// 输出: 层信息: 层305, 层205// 提取 305寝, 205寝var rooms FlexibleExtractor.ExtractByKeyword(text, 寝, Before);Console.WriteLine(寝信息: string.Join(, , rooms));// 输出: 寝信息: 305寝, 205寝}}总结‌最简单直接‌使用方案一的正则(层\d)|(\d寝)它可以一次性把所有符合这两种格式的片段都找出来。‌最灵活‌使用方案三通过参数控制是提取“前数字”还是“后数字”代码复用性高。‌关键点‌\d代表匹配任意长度的连续数字这解决了你提到的“数字是变化的”这一需求。如果您喜欢此文章请收藏、点赞、评论谢谢祝您快乐每一天。