Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/296.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.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,我想使用正则表达式创建一个基于起始字符和结束字符的字符串数组 举个例子可以帮助我解释。 将“$”视为下面字符串中的起始标识符,“|”视为结束标识符 堆栈$over |流$stack |交换 正则表达式应该在上面的字符串中找到over和stack [编辑以在OP的注释中包含代码片段…] string testingString = "stack $over| flow $stack| exchange"; var pattern = @"(?$.*?|)"; // also tried @"\$

我想使用正则表达式创建一个基于起始字符和结束字符的字符串数组

举个例子可以帮助我解释。 将“$”视为下面字符串中的起始标识符,“|”视为结束标识符

堆栈$over |$stack |交换

正则表达式应该在上面的字符串中找到overstack

[编辑以在OP的注释中包含代码片段…]

string testingString = "stack $over| flow $stack| exchange"; 
var pattern = @"(?$.*?|)"; // also tried @"\$[^|]\|" 
foreach (var m in System.Text.RegularExpressions.Regex.Split(testingString, pattern)) {     
    Response.Write(m ); 
} 
// output == stack $over| flow $stack| exchange 

在正则表达式中,$是一个特殊字符,表示“匹配字符串的结尾”。 对于需要转义的文本
$
,请尝试
\$

类似地,
|
是正则表达式中的特殊字符,需要转义

请尝试
\$.\\\\\\\\\$
\$[^ |]+\\\\\\\\

例如,从网上了解正则表达式

[更新] 作为对您的评论的响应,您希望提取由
$
|
分隔的文本,而不是对其进行拆分。尝试代替
Regex.Split

Regex t = new Regex(@"\$([^|]+)\|");
MatchCollection allMatches = t.Matches("stack $over| flow $stack| exchange");

在正则表达式中,$是一个特殊字符,表示“匹配字符串的结尾”。 对于需要转义的文本
$
,请尝试
\$

类似地,
|
是正则表达式中的特殊字符,需要转义

请尝试
\$.\\\\\\\\\$
\$[^ |]+\\\\\\\\

例如,从网上了解正则表达式

[更新] 作为对您的评论的响应,您希望提取由
$
|
分隔的文本,而不是对其进行拆分。尝试代替
Regex.Split

Regex t = new Regex(@"\$([^|]+)\|");
MatchCollection allMatches = t.Matches("stack $over| flow $stack| exchange");

我将使用lookbehind和lookaheads从匹配中排除起始分隔符和结束分隔符

string testingString = @"stack $over| flow $stack| exchange";

MatchCollection result = Regex.Matches
    (testingString,
            @"       
                (?<=\$)  # This is a lookbehind, it ensure there is a $ before the string
                [^|]*    # Match any character that is not a |
                (?=\|)   # This is a lookahead,it ensures that a | is ahead the pattern
            "
            , RegexOptions.IgnorePatternWhitespace);

foreach (Match item in result) {
    Console.WriteLine(item.ToString());
}
string testingString=@“stack$over | flow$stack | exchange”;
MatchCollection结果=Regex.Matches
(测试字符串,
@"       

(?我将使用look-behind和look-aheads从匹配中排除起始分隔符和结束分隔符

string testingString = @"stack $over| flow $stack| exchange";

MatchCollection result = Regex.Matches
    (testingString,
            @"       
                (?<=\$)  # This is a lookbehind, it ensure there is a $ before the string
                [^|]*    # Match any character that is not a |
                (?=\|)   # This is a lookahead,it ensures that a | is ahead the pattern
            "
            , RegexOptions.IgnorePatternWhitespace);

foreach (Match item in result) {
    Console.WriteLine(item.ToString());
}
string testingString=@“stack$over | flow$stack | exchange”;
MatchCollection结果=Regex.Matches
(测试字符串,
@"       

(?那么你应该…那样做。或者至少试着去做。($。?|)尝试过类似的事情,但我对规则表达知之甚少,这将是你问题的一个很好的补充。…那么你应该…那样做。或者至少试着去做。($。?)尝试过类似的方法,但我对正则表达式知之甚少,这将是对你的问题的一个很好的补充…我尝试过这两种方法,但都不起作用。我可以更改我的结束字符“|”。如果这是问题的原因,为什么不给出输入字符串、使用的确切代码和结果。这将帮助我们极大地帮助你。@mathematical.coffee在补全字符类之后,您忘记了一个
+
,否则您的答案有效。我尝试了这两种方法,但都不起作用。我可以更改结尾字符“|”。如果这是问题的原因,为什么不给出您的输入字符串、您使用的确切代码和结果。这将对我们有很大帮助。@material.coffee you在补全字符类之后忘记了
+
,否则您的答案有效