Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用正则表达式插入序列编号_C#_Regex - Fatal编程技术网

C# 使用正则表达式插入序列编号

C# 使用正则表达式插入序列编号,c#,regex,C#,Regex,目前(使用C#)我正在使用字符串方法Instr、Substring和Insert将节号放入字符串中。尽管这个方法可以正常工作,但它非常混乱,也不是很容易管理。我需要扩展它,因为还有其他的东西在后台进行。 我想知道是否可以使用正则表达式来插入节号,而不是使用标准的字符串方法?如果是,是如何做到的 例如: 原始字符串 敏捷的红狐。跳过那只懒狗?敏捷的红狐! “跳过”那只懒狗 如果使用正则表达式,则输出 1) 敏捷的红狐。2) 跳过那只懒狗?3) 快红 狐狸!“跳过”那只懒狗 PS:以下是我目前使用的

目前(使用C#)我正在使用字符串方法InstrSubstringInsert将节号放入字符串中。尽管这个方法可以正常工作,但它非常混乱,也不是很容易管理。我需要扩展它,因为还有其他的东西在后台进行。 我想知道是否可以使用正则表达式来插入节号,而不是使用标准的字符串方法?如果是,是如何做到的

例如:

原始字符串

敏捷的红狐。跳过那只懒狗?敏捷的红狐! “跳过”那只懒狗

如果使用正则表达式,则输出

1) 敏捷的红狐。2) 跳过那只懒狗?3) 快红 狐狸!“跳过”那只懒狗

PS:以下是我目前使用的不带正则表达式的方法:

public string[] ApplyContentNumbering(string[] lines)
{
    int count = 1;
    if (lines != null && lines.Length > 0)
    {
        lines[0] = String.Format("{0} {1}", Chapter, lines[0].Substring(0));
        bool isOpen = false;

        for (int index = 1; index < lines.Length; index++)
        {
            count++;
            lines[index] = String.Format("{0} {1}", count, lines[index].Substring(0));

            if (lines[index].IndexOf("\"") > 0)
            {
                for (int c = 0; c < lines[index].Length; c++)
                {
                    if (lines[index].Substring(c, 1).Equals("\""))
                    {
                        if (isOpen == false)
                        {
                            count++;
                            lines[index] = lines[index].Insert(c + 1, String.Format("{0}", count));
                            isOpen = true;
                        }
                        else
                        {
                            isOpen = false;
                        }
                    }
                }
            }
        }
    }
}
public string[]applycontentnumber(string[]行)
{
整数计数=1;
if(lines!=null&&lines.Length>0)
{
行[0]=String.Format(“{0}{1}”,章节,行[0]。子字符串(0));
bool-isOpen=false;
对于(int index=1;index0)
{
对于(int c=0;c
您可以在此处使用近似(或“足够好”)的方法,包括在字符串开头/标点符号之后添加递增的数字和1+空格,后跟大写字母

以下是一份:

正则表达式--匹配:

  • (^ |\p{p}\s+)
    -第1组捕获字符串或标点符号的开头和1+空格
  • (“?\p{Lu})
    -第2组捕获可选的
    ,然后捕获大写字母

要以更可靠的方式将文本拆分成句子,您最好使用一些NLP软件包。

要求是什么?何时插入数字?您好,Wiktor,我想在每个句子的开头和每个语音标记的开头添加数字,因此序列号不断递增。我不确定正则表达式是否是正确的方法。我相信代码可以从一些重构中受益,因为我不能一眼就看出它是如何工作的。将某些功能分离为独立于此特定方法的方法。比如一种把一行字分成句子的方法。然后,另一种方法是插入序列,等等。看一看-在正确的位置是
N)
(是的)看起来是正确的,突出显示的文本段也是正确的。我不确定如何增加编号?我是否需要对所有匹配项进行foreach并手动增加编号?我认为这可能是最好的选择?啊,你用linq来做替换,真漂亮!我从没想过你能做到。非常感谢。
var pat = @"(^|\p{P}\s+)(""?\p{Lu})";
var s = "The quick red fox. Jumped over the lazy dog? The quick red fox! \"Jumped over\" the lazy dog.";
var cnt = 0;
var res = Regex.Replace(s, pat, m =>
        string.Format("{0}{1}) {2}", m.Groups[1].Value, ++cnt, m.Groups[2].Value));
Console.WriteLine(res);
// => 1) The quick red fox. 2) Jumped over the lazy dog? 3) The quick red fox! 4) "Jumped over" the lazy dog.