我正在尝试在.NET中的同一正则表达式对象中匹配两个组,以便可以分别处理它们;我不想为每个单独的表达式实例化许多对象。基本上,我想在句点前插入下划线,在感叹号前插入破折号。现在,我知道我可以使用标点符号构造,但我想将每个组用作单独的表达式。
这是我尝试过的无数方法的一个变体:
using System.Text.RegularExpressions;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
var rule = new Regex(@"(\.)(\!)", RegexOptions.Compiled);
var myText = "string. will! way. water! test. quiz! short. long!";
richTextBox1.Text = rule.Replace(rule.Replace(myText, "_$1"), "-$2");
}
}
}
非常感谢您提前帮助。
$1
的内容。 - Alexei Levenkov