开发者

c# Regex substring after second time char appear

My problem is that I have a str开发者_如何学Cing in format like that:

dsadadsadas
dasdasda
dasda
4TOT651.43|0.00|651.43|98933|607.75|0.00|607.75|607.75|7621|14|0|0|799.42
dsda
dasad
das

I need to find the line that contains the 4TOT and substring the value between the socond and third '|' any ideas how I can obtain that in regex substring? For now I Have only that:

 var test = Regex.Match(fileContent, "4TOT.*").Value;

Which finds me entire line.


When the input is simple and follows a strict format like this, I usually prefer to use plain old string handling over regex. In this case it's spiced up with some LINQ for simpler code:

// filter out lines to use
var linesToUse = input
    .Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries)
    .Where(s => s.StartsWith("4TOT"));

foreach (string line in linesToUse)
{
    // pick out the value
    string valueToUse = line.Split('|')[2];

    // more code here, I guess
}

If you know that the input contains only one line that you are interested in, you can remove the loop:

string line = input
    .Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries)
    .Where(s => s.StartsWith("4TOT"))
    .FirstOrDefault();

string value = string.IsNullOrEmpty(line) ? string.Empty : line.Split('|')[2];

Update

Here is an approach that will work well when loading the input from a file instead:

foreach (var line in File.ReadLines(@"c:\temp\input.txt")
                         .Where(s => s.StartsWith("4TOT")))
{
    string value = string.IsNullOrEmpty(line) ? string.Empty : line.Split('|')[2];
    Console.WriteLine(value);
}

File.ReadLines is new in .NET 4 and enumerates the lines in the file without loading the full file into memory, but instead it reads it line by line. If you are using an earlier version of .NET you can fairly easily make your own method providing this behavior.


What about this regex? Seems to be working for me.

4TOT.*?\|.*?\|(.*?)\|

Captures the value you're looking for into a group.


Why don't you split your string twice: firstly with newline and then if target substring is found by '|' symbol without using of regex?

var tot = source.Split(Environment.NewLine.ToCharArray())
    .FirstOrDefault(s => s.StartsWith("4TOT"));

if (tot != null)
{
    // gets 651.43
    var result = tot.Split('|')
        .Skip(2)
        .FirstOrDefault();
}


Use the regex : ^4TOT(?:(?:[0-9]*(?:.[0-9]*)?)\|){2}([0-9]*(?:.[0-9]*)?).*

This regex will match 4TOT at the beginning followed by "2 numbers (decimal separated) then |" two times, and captures a number. The rest is ignored.

If you then use :

Match match = Regex.Match(input, pattern);

You will find the anwser into match.Groups

Memo: Numbers are [0-9]*\.[0-9]* Using the (?: ... ) makes a non-capturing parenthesis

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜