2

Text from txt file:

10 25
32 44
56 88
102 127
135 145
...

If it is a first line place 0, rest use the last number as a first in new line. Is it possible to do it or I need to loop through lines after regex parse.

0 10 25
25 32 44
44 56 88
88 102 127
127 135 145

(?<Middle>\d+)\s(?<End>\d+) //(?<Start>...)
6
  • 1
    Even if it's possible, why do you want to use regex for this, wouldn't that end up with a close to unreadable function? Commented Oct 17, 2017 at 20:53
  • 1
    Regex is the last tool I would use for this. Commented Oct 17, 2017 at 21:00
  • 1
    @Amy, I would use Regex after using my last tool available... Commented Oct 17, 2017 at 21:00
  • 1
    Well, this is somewhat doable in regex. Prepend 0 to the beginning of the text file, then use (.*?(\d+))([\r\n]) and replace with $1$3$2 . As others have stated, however, this is not the best tool to use. Commented Oct 17, 2017 at 21:10
  • 1
    You can't solve it with pure string pattern-replacement because the 0 you want to insert must exist on the matches line. So, all you can do is use a match evaluator in the Regex.Replace, like in this demo. Commented Oct 17, 2017 at 22:17

3 Answers 3

2

I would advise against using regex for readability reasons but this will work:

 var input = ReadFromFile();
 var regex = @"(?<num>\d*)[\n\r]+";
 var replace = "${num}\n${num} ";
 var output = Regex.Replace(input, regex, replace);

That will do everything apart from the first 0.

Sign up to request clarification or add additional context in comments.

Comments

1

Note that a regex approach does not sound quite good for a task like this. It can be used for small input strings, for larger ones, it is recommended that you write some more logic and parse text line by line.

So, more from academic interest, here is a regex solution showing how to replace with different replacement patterns based on whether the line matched is first or not:

var pat = @"(?m)(?:(\A)|^(?!\A))(.*\b\s+(\d+)\r?\n)";
var s = "10 25\n32 44\n56 88\n102 127\n135 14510 25\n32 44\n56 88\n102 127\n135 145";
var res = Regex.Replace(s, pat, m => m.Groups[1].Success ? 
        $"0 {m.Groups[2].Value}{m.Groups[3].Value} " : $"{m.Groups[2].Value}{m.Groups[3].Value} ");

Result of the C# demo:

0 10 25
25 32 44
44 56 88
88 102 127
127 135 14510 25
25 32 44
44 56 88
88 102 127
127 135 145

Note the \n line breaks are hardcoded, but it is still just an illustration of regex capabilities.

Pattern details

  • (?m) - an inline RegexOptions.Multiline modifier
  • (?:(\A)|^(?!\A)) - a non-capturing group matching either
    • (\A) - start of string capturing it to Group 1
    • | - or
    • ^(?!\A) - start of a line (but not string due to the (?!\A) negative lookahead)
  • (.*\b\s+(\d+)\r?\n) - Group 2:
    • .*\b - 0+ chars other than newline up to the last word boundary on a line followed with...
    • \s+ - 1+ whitespaces (may be replaced with [\p{Zs}\t]+ to only match horizontal whitespaces)
    • (\d+) - Group 3: one or more digits
    • \r?\n - a CRLF or LF line break.

The replacement logic is inside the match evaluator: if Group 1 matched (m.Groups[1].Success ?) replace with 0 and Group 2 + Group 3 values + space. Else, replace with Group 2 + Group 3 + space.

Comments

0

With C#.

var lines = File.ReadLines(fileName);

var st = new StringBuilder(); //or StreamWriter directly to disk ect.
var last  = "0";
foreach (var line in lines)
{
    st.AppendLine(last + " " + line );
    last = line.Split().LastOrDefault();
}

var lines2 = st.ToString();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.