【问题标题】:Create regex to find a sub-string that may or may not contain white space or new line? [closed]创建正则表达式以查找可能包含或不包含空格或换行符的子字符串? [关闭]
【发布时间】:2018-01-09 07:27:58
【问题描述】:

我有一个像下面这样的大字符串:

一个带有 content-start 内容的新字符串 content-end。带有 blah 的新字符串 blah.a new string with blah blah.

一个带有 blah blah 的新字符串。

一个带有 content-       开始的新字符串 content-end.

一个带有blah blah 的新字符串。一个带有blah blah 的新字符串。

带有内容的新字符串-
在此处开始
内容content-end

一个带有 blah blah 的新字符串。

我想分别匹配整个字符串 * 中的 content-startcontent-end 子字符串,即使它们之间有空格或换行符*

感谢任何有关此类正则表达式的输入。

【问题讨论】:

标签: c# regex string substring


【解决方案1】:

那个呢:

content\s*-\s*start(.*?)content\s*-\s*end

\s 考虑 \r \n \t \v 空间

此外,您也可以在您的 c# 正则表达式中执行此操作:

RegexOptions.Singleline | RegexOptions.IgnorePatternWhitespace
Regex.Matches(input, pattern, options)

//Which indicates dot matches newline
// RegexOptions.IgnorePatternWhitespace ignores whitespaces in pattern

Regex 101 Demo

【讨论】:

  • 这会考虑换行吗?
  • 是 \s 考虑 \r \n \t \v 空间
  • 不加理由就投反对票的原因是什么!!!
  • 如果 n 和 t 或 c 和 o 之间有空格怎么办?如何考虑?
  • c\s*o\s*n\s* ....你可以申请这个
猜你喜欢
  • 1970-01-01
  • 2014-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多