Regular expression - Avoiding characters
While using some regex in C#, I'm facing the following problem :
Consider this simple string : ~0~This is a simple text~POP~NIZ~0~0~
I would like to pick any strings between two '~', that contains more than 3 chars except of course the '~'. In my example, the would be :
This is a simple text
I could make something like : ([\w]|[\d]|.|\,.................){4-500}
I would end with a really long regex, impossible to debug and not readable...
Instead, I would prefer to create a regex like "Give me any ch开发者_运维百科aracters, except '~' that is contained between '~' and '~' ".
I can't find a way to use [^] properly !
How can I do this ?
Thanks in advance !
ANSWER : I've finally done this : ~[^~]{3,}~
It takes everything but '~', contained between two '~' and that is more than three chars long.
Thanks for your answers !
If you don't mind a possible extra batch from the start and the end, it should be as easy as:
[^~]{3,}
Or, you can just split and take the long ones:
var tokens = str.Split('~').Where(s => s.Length >= 3);
If you do want to limit the characters to a specific set, you can use lookahead and behind to make sure. This will not consume the tilde signs, so you get two matches for ~123~abc~
(again, you can use [^~]
if you are move comfortable with it):
(?<=~)[\w\d\s]{3,}(?=~)
Try this regex (?:~([^~]{3,})~)
It will match everithing between two ~~ (wont catch ~)
Something like:
~([^~]{3}[^~]+)~
(tested)
精彩评论