Take the 2-minute tour ×

I need regexp for expression: "dsfsdf|dfdfdf|Gfgfgfg|ghghfgh|fgfjghjg" Between "|" may be any sequences of symbols.

I have tried this expression: [a-z]+\|[a-z]+ but it contains similar code ([a-z]+). I want expression without copy-paste because in fact my expression is more complex.

Thanks in advance...

share|improve this question
What exactly do you want to achieve? To split the string or just test it against the pattern? –  Alex Apr 5 '11 at 10:07
just test it against the pattern –  e.gluhotorenko Apr 5 '11 at 10:10
add comment

3 Answers

up vote 0 down vote accepted



The (?:) is a non capturing group and the \|? is looking for one or 0 |

You can add any character you are looking for between the []. There are also predefined character sets like \w for word characters (I am not sure what is defined as a word character within c# .net at least A-Za-z and _ )

share|improve this answer
Your solution finds expression like this: "qweqe|qw|eqwerer|" too, but i need "|" only in middle. –  e.gluhotorenko Apr 5 '11 at 10:17
Then I am sure there is a solution using a lookahead assertion (I am sorry, not getting it to work at the moment), but this is also a complex expression. I think your expression [a-z]+\|[a-z]+ is then easier to read. And I would not name it copy-paste what you are doing here, you are describing your pattern. –  stema Apr 5 '11 at 11:22
add comment

I think you need the next expression:


it matches a string if it isn't begins or ends with the | symbol.

share|improve this answer
add comment

Why not just use string.Split('|')?

If you want to verify that your string is valid use [a-z|]+

share|improve this answer
Я думаю, что легче написать одну регулярку и проверить все, чем сплитать и потом каждую отдельно верифаить –  e.gluhotorenko Apr 5 '11 at 10:15
там не только буквы, там все сложнее –  e.gluhotorenko Apr 5 '11 at 10:22
Уже понял. тогда не знаю. Наверное то что stema предложил, но после | сделать look ahead что это не конец строки –  Poma Apr 5 '11 at 10:24
add comment

Your Answer


