Regular Expression - Avoidance of Characters

When using some regex in C #, I have the following problem:

Consider this simple line: ~ 0 ~ This is plain text ~ POP ~ NIZ ~ 0 ~ 0 ~

I would like to select any lines between two "~" containing more than three characters, except, of course, "~". In my example, this would be:

This is plain text.

I could do something like: ([\ w] | [\ d] |. | \, .................) {4-500}

I would end a very long regular expression, it is impossible to debug and not read ...

Instead, I would prefer to create a regular expression, for example, "Give me any characters except" ~ ", which is between" ~ "and" ~ " .

I can’t find a way to use [^] correctly!

How can i do this?

Thanks in advance!

ANSWER: I finally did this: ~ [^ ~] {3,} ~

Everything except '~' is required, contained between two ~ ~ characters and more than three characters long.

Thank you for your responses!

+3
source share
3 answers

If you do not mind a possible additional batch from the very beginning and the end, it should be as simple as:

[^~]{3,}

Or you can just split and take long ones:

var tokens = str.Split('~').Where(s => s.Length >= 3);

, lookahead , . , ~123~abc~ ( , [^~], ):

(?<=~)[\w\d\s]{3,}(?=~)
+4

(?:~([^~]{3,})~)
~~ (wont catch ~)

+2

Sort of:

~([^~]{3}[^~]+)~

(verified)

0
source

Source: https://habr.com/ru/post/1778057/


All Articles