Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.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_String Split - Fatal编程技术网

C# 按分隔符拆分,但不将其从字符串中删除

C# 按分隔符拆分,但不将其从字符串中删除,c#,regex,string-split,C#,Regex,String Split,我想用一个正则表达式把长字符串分割成单独的行。 行可以包括任何可能的unicode字符。 行在点(“.”-一个或多个)或新行(“\n”)上“结束” 示例: 此字符串将作为输入: "line1. line2.. line3... line4.... line5..... line6 \n line7" 输出: “第1行。” “第2行…” “第3行…” “第4行…” “第5行…” “第6行” “第7行” 试试这个: String result = Regex.Replace(subject, @

我想用一个正则表达式把长字符串分割成单独的行。 行可以包括任何可能的unicode字符。 行在点(“.”-一个或多个)或新行(“\n”)上“结束”

示例:

此字符串将作为输入:

"line1. line2.. line3... line4.... line5..... line6
\n
line7"
输出:

  • “第1行。”
  • “第2行…”
  • “第3行…”
  • “第4行…”
  • “第5行…”
  • “第6行”
  • “第7行”
试试这个:

String result = Regex.Replace(subject, @"""?(\w+([.]+)?)(?:[\n ]|[""\n]$)+", @"""$1""\n");

/*
"line1."
"line2.."
"line3..."
"line4...."
"line5....."
"line6"
"line7"
*/
正则表达式解释

"?(\w+([.]+)?)(?:[\n ]|["\n]$)+

Match the character “"” literally «"?»
   Between zero and one times, as many times as possible, giving back as needed (greedy) «?»
Match the regular expression below and capture its match into backreference number 1 «(\w+([.]+)?)»
   Match a single character that is a “word character” (letters, digits, and underscores) «\w+»
      Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
   Match the regular expression below and capture its match into backreference number 2 «([.]+)?»
      Between zero and one times, as many times as possible, giving back as needed (greedy) «?»
      Match the character “.” «[.]+»
         Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Match the regular expression below «(?:[\n ]|["\n]$)+»
   Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
   Match either the regular expression below (attempting the next alternative only if this one fails) «[\n ]»
      Match a single character present in the list below «[\n ]»
         A line feed character «\n»
         The character “ ” « »
   Or match regular expression number 2 below (the entire group fails if this one fails to match) «["\n]$»
      Match a single character present in the list below «["\n]»
         The character “"” «"»
         A line feed character «\n»
      Assert position at the end of the string (or before the line break at the end of the string, if any) «$»

如果您希望保持所有点的完整性,并且点后面会有一个空白,那么这可以是您的正则表达式:

String result = Regex.Replace(t, @".\s", @".\n");

这将是一个字符串。您没有说明是需要更多字符串还是需要一个字符串。

如果我理解您的要求,您可以尝试以下模式:

(?<=\.)(?!\.)|\n
产生

line1. 
 line2.. 
 line3... 
 line4.... 
 line5..... 
 line6 
line7 
如果您想消除空白,只需将其更改为:

(?<=\.)(?!\.)\s*|\n

(?从您的示例来看,您似乎只想在空白处拆分?例如inputString.split(新字符[]{','\r','\n','\t'},StringSplitOptions.RemoveEmptyEntries)
(?<=\.)(?!\.)\s*|\n
(?<=\.)\s+|\n