user518363
user518363

Reputation: 185

C# Regular Expression to find characters between Space and |

I want to find characters that should come between space and |.

I am using the below expression but I could not get correct output.

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

Upvotes: 0

Views: 75

Answers (3)

Denim Datta
Denim Datta

Reputation: 3802

You can use this one :

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

Anubhava's answer is also correct, but in case of

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

this regex has three parts:

  • (?<=\s) : look-back for one space
  • [^\|]* : anything other than |
  • (?=\|) : look-ahead for |

so, together (?<=\s)[^\|]*(?=\|) will match the sequence which has a space( ) before it, and a | after, without including them in the match.

You can test the regex here.

Upvotes: 4

Ahmad Hussain
Ahmad Hussain

Reputation: 2489

Use this regular expression:

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

You can test this regular expression on Rubular

Upvotes: 0

anubhava
anubhava

Reputation: 786329

You can use this regex:

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

Upvotes: 1

Related Questions