gmsantos
gmsantos

Reputation: 1419

Regex not working as should in .Net

I trying to replace some line breaks from a file in C#, given a specific pattern:

(?m)(^\t[0-9A-Z#].+$)\r?\n

File example structure

    1   1   1   1
ab  as  as  
    123 1   2
        13
    32  3   12  2
ds  ds  12

Applying $1\t as replace, I expect to get the following result:

    1   1   1   1   ab  as  as  
    123 1   2           13
    32  3   12  2   ds  ds  12

In fact, this replace is working properly at regexr.

But in C# the file simply return no matches. Is any particularity in this regex that .net matches differentially?

public void masterDataEleb()
{
    // Tried both with (?m) and RegexOptions.Multiline, not working
    Regex masterDataRegex = new Regex(@"(^\t[0-9A-Z#].+$)\r?\n", RegexOptions.Multiline);
    string replaceTo = "$1\t";

    string textFile = File.ReadAllText(filePath, Encoding.Default);
    textFile = masterDataRegex.Replace(textFile, replaceTo);

    File.WriteAllText(filePath, textFile, Encoding.Default);
}

Upvotes: 1

Views: 127

Answers (2)

Michael Petito
Michael Petito

Reputation: 13171

At the end of your expression (ignoring your capture group), you have the sequence:

$\r?\n

When you use RegexOptions.Multiline in .NET, the $ anchor greedily matches LF. Your match fails because there is no sequence LFCRLF or LFLF in your file.

You should instead try using the pattern \r?$ to match the end of your line.

The reason your pattern does work at regexr may be a difference in behavior of the $ anchor (for example, the behavior at regexr appears to match $ before the LF, although I suspect it may fail with input lines ending in CRLF for similar reasons).

Upvotes: 1

Braj
Braj

Reputation: 46891

This might help you. substitute with $1\t$2$3

([^\r\n]*)\r?\n([^\r\n]*)(\r?\n?)

Online demo

Pattern explanation:

([^\r\n]*)    Group everything till new line is found
\r?\n         New line is found
([^\r\n]*)    Group everything till new line is found
(\r?\n?)      New line is found(optional)

Upvotes: 0

Related Questions