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:

  1. if want "collapse" multiple newlines 1 original regex hints (with + sign), wrap entire regex in non-capturing group plus: (?:(?: )?\r\n)+
  2. yes, regex replace double-space-line-break exact same thing, ok , better adding spaces, mentioned.
  3. adding same character character class (using [brackets]) multiple times has no meaning. [ ] same [ ], means "match either space or space or space...".

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -