c# - Trouble with exceptions using regex -
i have line of code:
textbox1.text = regex.replace(textbox1.text, "(?:\r\n)+", " \r\n");
which adds double spacebar line before line break:
input:
a b c d e
output:
a //<--- double spacebar after 'a' character b //<--- double spacebar after 'b' character c //<--- double spacebar after 'c' character d //<--- double spacebar after 'd' character e
for formatting purposes on pages 1 or reddit, need either double break line or double spacebar in previous line , single line break afterwards make new line in formatting
anyway, it's working, problem if have double spacebar after line, keeps adding , results in line many spacebars, unnecesary because need 2 work
so i've tried doing exception [^ ], should not consider rule if has double spacebar before, this:
(?:[^ ]\r\n)+
but doesn't work?
input:
a b c //<---- double spacebar before line break check if ignores d e
output:
//<--- double spacebar here??
//<--- blank line, nothing there
//<--- double spacebar here??
c //<--- double spacebar here??
//<--- double spacebar here??
e
why? what's wrong? thank much
the correct regex: (?: )?\r\n
some points consider:
- if want "collapse" multiple newlines 1 original regex hints (with + sign), wrap entire regex in non-capturing group plus:
(?:(?: )?\r\n)+
- yes, regex replace double-space-line-break exact same thing, ok , better adding spaces, mentioned.
- adding same character character class (using [brackets]) multiple times has no meaning.
[ ]
same[ ]
, means "match either space or space or space...".
Comments
Post a Comment