tags:

views:

140

answers:

2

What is a regular expression for

initSock|north|router\r\n

Where there is north changes all the time but the rest is always the same. So It can be:

initSock|foo|router\r\n
initSock|bar|router\r\n

etc.

But where north, foo or bar are there definitely should be something and can't be empty. I am going to use this in C#.

To make it more clear: Any thing that looks like this is bad:

initSock||router\r\n
initSockfoorouter\r\n
|foo|router\r\n
initSock|foo|router
initSock|foo|portal\r\n
outSock|foo|router\r\n
+7  A: 
"^initSock\|([a-zA-Z]+)\|router\\r\\n$"

Although I would probably be tempted to split on the pipe ("|") character...

David Grant
Deleted my post. While I'm not sure, you seem to have figured out what he was asking better than I had.
+4  A: 

To extend the above answer from M Potato Head to allow for all possible characters I'd rewrite the regexp as:

"^initSock\|[^|]+\|router\\r\\n$"

or

"^initSock\|[^|][^|]*\|router\\r\\n$"

if you can't use extended regexp. That way you don't have to ensure that you've allowed for all possible chars in the set of characters that you're using to match the varying part of the name.

HTH

cheers,

Rob

Rob Wells
The OP didn't specify whether he wanted to capture the term or not. Mr. Potato Head's response captures and yours does not. Might be useful to the OP to point out that difference.
Adam Bellaire
@Adam, aren't you assuming Perl there?
Rob Wells
...and thee's no mention of wanting to capture the varying component in the OP's quesion.
Rob Wells
If I need to change | to # do I need to use ...[^#][^#]*\... ?
Ali
@Ali, yes. That part of the regexp says match any single character except for '|'. Adding the caret '^' character at the beginning of the character class changes its meaning.Don't forget to have \#[^#][^#]*\# around it.
Rob Wells