How can I fix this regex expression?

前端 未结 3 1896
萌比男神i
萌比男神i 2021-01-28 19:12

Preface: This question is a derivative of this question.


Here is my code:

using System;
using System.Linq;
using System.Text.RegularExpressions;

c         


        
相关标签:
3条回答
  • 2021-01-28 19:48

    Use negative lookbehind, positive lookbehind, character class with quanitifer, positive lookahead, and negative lookahead.

    Working Demo

    using System;
    using System.Linq;
    using System.Text.RegularExpressions;
    
    class MainClass {
      public static void Main (string[] args) {
            const string rawLine = "\"TeamName\",\"PlayerName\",\"Position\"  \"Chargers\",\"Philip Rivers\",\"QB\"  \"Colts\",\"Peyton Manning\",\"QB\"  \"Patriots\",\"Tom Brady\",\"QB\"";
                var parsedLines = Regex.Split(rawLine, "(?<![,])(?<=[\"])[ ]{2}(?=[\"])(?![,])");
                parsedLines.ToList().ForEach(Console.WriteLine);
    
                Console.WriteLine("Press [ENTER] to exit.");
                Console.ReadLine();
      }
    }
    
    0 讨论(0)
  • 2021-01-28 19:50

    As Amy has already mentioned, your string seems to be something like CSV. If it is really a valid CSV - use special libraries.

    If CSVHelper isn't applicable in this case and you really need regex, try something like this one:

    (?<=(?:^|  ))(.*?)(?=(?:  \")|$)
    

    I haven't coded for C#, so regex may need some edits due to c# specific.

    Edit. Code example.

    using System;
    using System.Linq;
    using System.Text.RegularExpressions;
    
    class MainClass {
      public static void Main (string[] args) {
            const string rawLine = "\"TeamName\",\"PlayerName\",\"Position\"  \"Chargers\",\"Philip Rivers\",\"QB\"  \"Colts\",\"Peyton Manning\",\"QB\"  \"Patriots\",\"Tom Brady\",\"QB\"";
                //var parsedLines = Regex.Split(rawLine, "(?<=(?:^|  ))(.*?)(?=(?:  \")|$)");
          var parsedLines = Regex.Split(rawLine, "(?<=^)(.*?)(?=(?:  \")|$)|(?<=  )(.*?)(?=(?:  \")|$)");
                parsedLines.ToList().ForEach(Console.WriteLine);
    
                Console.WriteLine("Press [ENTER] to exit.");
                Console.ReadLine();
      }
    }
    

    This code with "dirty" fix for assertion error. However, i can't reproduce it with onlinetool :) Original regex commented in this example.

    I hope, this will help you. But i must say again if you working with csv - it is better to use special tools, not regex :)

    0 讨论(0)
  • 2021-01-28 19:50

    Good comments through-out the thread (I would strongly suggest pursuing one of those options), I wont focus on them. Here's an alternative solution that uses Matches from the Regex pattern, skip how many fields you have (columns) and then take how many records you want.

    I'm using a pattern like (\"(.*?)[^,]") and explanation can be found here of what it means.

    const string rawLine = "\"TeamName\",\"PlayerName\",\"Position\"  \"Chargers\",\"Philip Rivers\",\"QB\"  \"Colts\",\"Peyton Manning\",\"QB\"  \"Patriots\",\"Tom Brady\",\"QB\"";                       
    var matches = new Regex(@"(\""(.*?)[^,]"")").Matches(rawLine).Cast<Match>().ToList();
    // loop through our matches
    for(int i = 0; i < matches.Count; i++)
    {                
        // join our records we need to output
        string str = string.Join(",", matches.Skip(i * 3).Take(3));
        if(!string.IsNullOrEmpty(str))
             Console.WriteLine(str);
    }            
    Console.WriteLine("Press [ENTER] to exit.");
    Console.ReadLine();
    

    Please note, there's no error checking at all, can be improved, but does produces the output you need. *Also make sure you import System.Linq if not already there.

    Output Test

    0 讨论(0)
提交回复
热议问题