Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to skip a column?

Tags:

c#

regex

I have textual table:

   13.5         0.12557         0.04243         -0.0073         0.00377
     14         0.12573            0.05        -0.00697         0.00437
   14.5         0.12623         0.05823        -0.00703           0.005
     15         0.12853          0.0686        -0.00627         0.00493
   15.5          0.1299         0.08073        -0.00533          0.0063

where I would like to match all numbers, except those in first column. I've tried to use negative lookbehind without success:

(?<!^)[\d.E-]+

How to match all numbers except those in first column(13.5, 14, 14.5, 15, 15.5)?

like image 593
Pablo Avatar asked Oct 29 '22 20:10

Pablo


1 Answers

Note that in case you do not care about data validation, you may do without a regex here:

var results = line.Split(new[] {"\r", "\n"}, StringSplitOptions.RemoveEmptyEntries)
            .Select(v => v.Split(new[] {" "}, StringSplitOptions.RemoveEmptyEntries).Skip(1))
            .SelectMany(x => x);
Console.WriteLine(string.Join(",",results));
// => 0.12557,0.04243,-0.0073,0.00377,0.12573,0.05,-0.00697,0.00437,0.12623,0.05823,-0.00703,0.005,0.12853,0.0686,-0.00627,0.00493,0.1299,0.08073,-0.00533,0.0063

In case you want to only match those numbers when the data are numbers, you may use

    var results2 = Regex.Matches(line, @"(?m)(?<!^[\p{Zs}\t]*)(?<=[\p{Zs}\t])-?[0-9]*\.?[0-9]+(?:[eE][-+]?[0-9]+)?")
            .Cast<Match>()
            .Select(m => m.Value);

See the C# demo.

The -?[0-9]*\.?[0-9]+(?:[eE][-+]?[0-9]+)? float/integer number regex is explained here.

Pattern details

  • (?m) - a multiline modifier that makes ^ match start of the line
  • (?<!^[\p{Zs}\t]*) - a negative lookbehind that fails the match if there is a start of the string followed with 0+ horizontal whitespace chars after it imediately to the left of the current location
  • (?<=[\p{Zs}\t]) - a positive lookbehind that requires a horizontal whitespace before a
  • -?[0-9]*\.?[0-9]+(?:[eE][-+]?[0-9]+)? - integer/float number regex:
    • -? - an optional - char
    • [0-9]* - 0+ digits
    • \.? - an optional . char
    • [0-9]+ - 1+ digits
    • (?:[eE][-+]?[0-9]+)? - an optional sequence of:
      • [eE] - a e or E char
      • [-+]? - an optional - or + char
      • [0-9]+ - 1+ digits.
like image 61
Wiktor Stribiżew Avatar answered Nov 15 '22 08:11

Wiktor Stribiżew