How can I use lookbehind in a C# Regex in order to remove line breaks? -
i have text file repetitve structure header , detail records such as
stopservice:: 697::12::test::20::a@yahoo.com::20 main rd::alcatraz::ca::1200::please send me information a@gmail.com::0::::
i want remove line break between header , detail record process them single record, detail record can contain line breaks need remove line breaks follow directly ::
sign.
i'm not pro when using regular expressions searched , tried use approach doesn't work:
string text = file.readalltext(path); regex.replace(text, @"(?<=(:))(?!\1):\n", string.empty); file.writealltext(path, text);
i tried this:
regex.replace(text, @"(?<=::)\n", string.empty);
any idea how can use regex look-behind in case? output should this:
stopservice::697::12::test::20::a@yahoo.com::20 main rd::alcatraz::ca::1200::please send me information a@gmail.com::0::::
non-regex way
read file line line. check first line , if equal stopservice::
not add newline (environment.newline
) after it.
regex way
you can match line break after first ::
using (?<=^[^:]*::)
look-behind:
var str = "stopservice::\r\n697::12::test::20::a@yahoo.com::20 main rd::alcatraz::ca::1200::please send me information to\r\na@gmail.com::0::::"; var rgx = new regex(@"(?<=^[^:]*::)[\r\n]+"); console.writeline(rgx.replace(str, string.empty));
output:
stopservice::697::12::test::20::a@yahoo.com::20 main rd::alcatraz::ca::1200::please send me information a@gmail.com::0::::
see ideone demo
the look-behind ((?<=...)
) matches:
^
- start of string[^:]*
- 0 or more characters other:
::
- 2 colons
the [\r\n]+
pattern makes sure match newline symbols, if there more one.
Comments
Post a Comment