C#正则表达式查找空格和|之间的字符

时间:2013-11-21 10:24:00

标签: c# regex

我想找到空格和|。

之间的字符

我使用的是以下表达式,但无法获得正确的输出。

@"\s\S*[|]\b"

3 个答案:

答案 0 :(得分:4)

你可以使用这个:

(?<=\s)[^\|]*(?=\|)
Anubhava的答案也是正确的,但是在

的情况下
String : "Helllo good day |on a go|there you are"
Match :  "good day ", "a go", "you are"           // Anubhava's
Match :  "good day ", "a go"                      // this one

// "you are" should not be matched as not in between space and |. | is not there at the end

这个正则表达式有三个部分:

  • (?<=\s):回顾一个空间
  • [^\|]*:除<| li>以外的任何内容
  • (?=\|):向前看|

所以,(?<=\s)[^\|]*(?=\|)合在一起会匹配前面有 space( ) 的序列,之后有 | 的序列把他们包括在比赛中。

你可以test the regex here

答案 1 :(得分:1)

您可以使用此正则表达式:

"(?<=\s)[^|]*"

答案 2 :(得分:0)

使用此正则表达式:

/\ ([^ \|]*)\|/

您可以在Rubular

上测试此正则表达式