# 正则问题：如何表示一个带量词的分组的第n个匹配Capture？

**URL:** <https://forum.freemdict.com/t/topic/23853>\
**Category:** 技术交流与词典编修\
**Created:** [October 5, 2023, 9:34am UTC](https://forum.freemdict.com/t/topic/23853 "2023-10-05T09:34:45Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![webeyes](https://forumcdn.freemdict.com/letter_avatar_proxy/v4/letter/w/c0e974/32.png) [@webeyes](https://forum.freemdict.com/u/webeyes)\
**Post date:** [October 5, 2023, 9:34am UTC](https://forum.freemdict.com/t/topic/23853/1 "2023-10-05T09:34:45Z")

</div>

实例：  
字符串： This is my personal dictionary.  
正则表达式： (?\w+[\s.])+  
该正则表达式匹配整个字符串，总共算仅有1个匹配。命名分组word后面有一个量词+， 该分组在仅有的这个匹配(match)中被匹配(capture)5次。  
注意：我用了match和capture来分别表达整个正则表达式的仅1次“匹配”和分组的多次“匹配”，以避免混淆

目的：在C#（或其他编程语言）编程中或者在emedit或notepad++等文本编辑器中使用字符串替换，需要替换该分组的第3个(从0开始)capture为原captured子字符串后加上"end"，即最后结果为： This is my personalend dictionary.

我是在C#编程中遇到这个需求的，程序中需要进行大量正则替换，每次替换都需要引用第n个capture来得到替换后字符, 这个n不是一个固定值而是每次替换都会动态变化，每次正则替换时需要的正则表达式都要以n为参数动态构建，所以我需要有一种通用的正则表达式语法可以引用第n个capture（如果n在每次替换中都是一个固定的常数就可以用其他方法简单解决了）

找了不少资料竟然没有找到！难道不存在这种引用表达方法吗？？

最后，我终于在C# API中找到了Capture Class 基本达到了我的目的，但仍不完美！因为如果存在引用第n个capture的语法，可以直接在以下方法中的replacement这个参数中用上，本来下面一条语句就可以完成工作的：  
Replace (string input, string pattern, string replacement)  
但现在是不存在这种语法，我不得不迂回方式调用以下方法：  
Replace (string input, string pattern, MatchEvaluator evaluator); // evaluator是回调方法，对每个匹配都会被系统自动调用  
然后我需要编程回调方法中的具体内容,在命名分组上调用Captures[n] 回调方法并不难写，但是C#的这个API做得不方便，回调方法中仅允许一个固定类型match的形参，导致其它参数要使用的话只能改成全局变量，加大了各个方法的耦合程度，非常不优雅。

参考资料：是C#的，其他语言PYTHON是类似的，但C#正则API上确实是所有语言中最最强大的没有之一

> **[Regular Expression Language - Quick Reference - .NET](https://learn.microsoft.com/en-us/dotnet/standard/base-types/regular-expression-language-quick-reference)**
>
> In this quick reference, learn to use regular expression patterns to match input text. A pattern has one or more character literals, operators, or constructs.

> **[Substitutions in Regular Expressions - .NET](https://learn.microsoft.com/en-us/dotnet/standard/base-types/substitutions-in-regular-expressions)**
>
> Make substitutions to replace matched text using regular expressions in .NET. Substitutions are language elements recognized only within replacement patterns.

> **[Grouping Constructs in Regular Expressions - .NET](https://learn.microsoft.com/en-us/dotnet/standard/base-types/grouping-constructs-in-regular-expressions)**
>
> Learn to use grouping constructs in .NET. Grouping constructs delineate subexpressions of a regular expression and capture substrings of an input string.

> **[CaptureCollection Class (System.Text.RegularExpressions)](https://learn.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.capturecollection?view=net-7.0)**
>
> Represents the set of captures made by a single capturing group. The collection is immutable (read-only) and has no public constructor.

> **[Match.Groups Property (System.Text.RegularExpressions)](https://learn.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.match.groups?view=net-7.0)**
>
> Gets a collection of groups matched by the regular expression.

> **[GroupCollection Class (System.Text.RegularExpressions)](https://learn.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.groupcollection?view=net-7.0)**
>
> Returns the set of captured groups in a single match. The collection is immutable (read-only) and has no public constructor.

> **[Match Class (System.Text.RegularExpressions)](https://learn.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.match?view=net-7.0)**
>
> Represents the results from a single regular expression match.

注意：我的这个具体问题仅为说清我想要找到一种什么样的正则表达式的语法，该问题本身并非关注点，它显然可以用其他简单方法解决。
