Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# =@“|业主|行动|状态|评论| | | | | | | | | | | | | | | | | | | | | | | | | | |业主| | | | |行动| | | | | |; //获取标题 var regHeaders=new Regex(@“\\\\\\\\\\\s*(\w[^\\\\\\]+)”,RegexOptions.Compiled); var headers=regHeaders.Matches(文本); //根据标题列的数量获取行 var regLinhas=new Regex(String.Format(@“(?:\\\\\s*(\w[^\\\\]+){{{{0}}}”,headers.Count)); var rows=regLinhas.Matches(文本); var tbl=新数据表(); foreach(在标题中匹配标题) { tbl.Columns.Add(header.Groups[1].Value); } foreach(按行匹配行) { tbl.Rows.Add(row.Groups[1].Captures.OfType().Select(col=>col.Value.ToArray()); }_C#_Wiki_Confluence - Fatal编程技术网

C# =@“|业主|行动|状态|评论| | | | | | | | | | | | | | | | | | | | | | | | | | |业主| | | | |行动| | | | | |; //获取标题 var regHeaders=new Regex(@“\\\\\\\\\\\s*(\w[^\\\\\\]+)”,RegexOptions.Compiled); var headers=regHeaders.Matches(文本); //根据标题列的数量获取行 var regLinhas=new Regex(String.Format(@“(?:\\\\\s*(\w[^\\\\]+){{{{0}}}”,headers.Count)); var rows=regLinhas.Matches(文本); var tbl=新数据表(); foreach(在标题中匹配标题) { tbl.Columns.Add(header.Groups[1].Value); } foreach(按行匹配行) { tbl.Rows.Add(row.Groups[1].Captures.OfType().Select(col=>col.Value.ToArray()); }

C# =@“|业主|行动|状态|评论| | | | | | | | | | | | | | | | | | | | | | | | | | |业主| | | | |行动| | | | | |; //获取标题 var regHeaders=new Regex(@“\\\\\\\\\\\s*(\w[^\\\\\\]+)”,RegexOptions.Compiled); var headers=regHeaders.Matches(文本); //根据标题列的数量获取行 var regLinhas=new Regex(String.Format(@“(?:\\\\\s*(\w[^\\\\]+){{{{0}}}”,headers.Count)); var rows=regLinhas.Matches(文本); var tbl=新数据表(); foreach(在标题中匹配标题) { tbl.Columns.Add(header.Groups[1].Value); } foreach(按行匹配行) { tbl.Rows.Add(row.Groups[1].Captures.OfType().Select(col=>col.Value.ToArray()); },c#,wiki,confluence,C#,Wiki,Confluence,这里有一个涉及正则表达式的解决方案。它接受单个字符串作为输入,并返回标题列表和行/列列表>。它还修剪空白,这可能是或可能不是理想的行为,所以要注意这一点。它甚至可以很好地打印东西:) 使用系统; 使用System.Collections.Generic; 使用System.Text.RegularExpressions; 命名空间解析Wiki { 班级计划 { 静态void Main(字符串[]参数) { 字符串内容=@“| |业主| |行动| |状态| | |评论| | | | | | |

这里有一个涉及正则表达式的解决方案。它接受单个字符串作为输入,并返回标题列表和行/列列表>。它还修剪空白,这可能是或可能不是理想的行为,所以要注意这一点。它甚至可以很好地打印东西:)

使用系统;
使用System.Collections.Generic;
使用System.Text.RegularExpressions;
命名空间解析Wiki
{
班级计划
{
静态void Main(字符串[]参数)
{
字符串内容=@“| |业主| |行动| |状态| | |评论| | | | | | | | | | | | | | | | | | | | | | |业主| | | | | | |行动| | |;
content=content.Replace(@“\\”,“”);
string headerContent=content.Substring(0,content.LastIndexOf(“| |”)+2);
string cellContent=content.Substring(content.LastIndexOf(“| |”)+2);
MatchCollection headerMatches=new Regex(@“\\\\\\\\”([^ \\]*)(?=\\\\\\\\\)”,RegexOptions.Singleline)。匹配(headerContent);
MatchCollection cellMatches=新正则表达式(@“\\\;([^ 124;]*)(?=\\\\\)”,RegexOptions.Singleline)。匹配(cellContent);
列表标题=新列表();
foreach(头部匹配)
{
如果(match.Groups.Count>1)
{
headers.Add(match.Groups[1].Value.Trim());
}
}
列表体=新列表();
List newRow=新列表();
foreach(单元格匹配)
{
if(newRow.Count>0&&newRow.Count%headers.Count==0)
{
body.Add(newRow);
newRow=新列表();
}
其他的
{
newRow.Add(match.Groups[1].Value.Trim());
}
}
body.Add(newRow);
打印(标题、正文);
}
静态无效打印(列表标题、列表正文)
{
变量单元大小=20;
对于(int i=0;iif(string.IsNullOrEmpty(value)| | value.Length是源中的\还是在示例中突出显示扩展行?否..如果我将其读入原始字符串,整个内容将显示为一行。我在上面对其进行了分解,以便更容易地查看我想要查看的表您做了哪些工作?我已用我尝试过的内容更新了问题。我已经让它工作了我正在尝试我的第二种方法,但我觉得可能有一种更简单的方法来实现this@leora您提到原始数据以单行形式出现,但您已为问题设置了格式。请将原始输入添加到您的问题中。请查看我的更新问题,因为我在尝试此解决方案时包含了未设置任何格式的原始文本,并且在dn似乎没有给我正确的列或行数据。我想我的答案需要一些代码示例,但我不明白为什么要在答案中添加引用链接。它没有指向任何示例代码或更详细的解释。完全正确。那是一个不相关的链接,不应该添加。我回滚了更改。在fu中您可以随意回滚这些编辑。只需单击“已编辑”链接,您将看到日志-历史允许您回滚内容。
 || Owner || Action || Status || Comments ||
 | Bill | Fix the lobby | In Progress | This is easy |
 | Joe | Fix the bathroom | In Progress | Plumbing \\
 \\
  Electric \\
 \\
 Painting \\
 \\
 \\ | 
 | Scott | Fix the roof | Complete | This is expensive |
|| Owner|| Action || Status || Comments || | Bill\\ | fix the lobby |In Progress | This is eary| | Joe\\ |fix the bathroom\\ | In progress| plumbing  \\Electric \\Painting \\ \\ | | Scott \\ | fix the roof \\ | Complete | this is expensive| 
List<List<string>> table = new List<List<string>>();


var match = Regex.Match(raw, @"(?:(?:\|\|([^|]*))*\n)?");
if (match.Success)
{
    var headersWithExtra = match.Groups[1].Captures.Cast<Capture>().Select(c=>c.Value);
    List<String> headerRow = headersWithExtra.Take(headersWithExtra.Count()-1).ToList();
    if (headerRow.Count > 0)
    {
        table.Add(headerRow);
    }
}

match = Regex.Match(raw + "\r\n", @"[^\n]*\n" + @"(?:\|([^|]*))*");
var cellsWithExtra = match.Groups[1].Captures.Cast<Capture>().Select(c=>c.Value);

List<string> row = new List<string>();
foreach (string cell in cellsWithExtra)
{
    if (cell.Trim(' ', '\t') == "\r\n")
    {
        if (!table.Contains(row) && row.Count > 0)
        {
            table.Add(row);
        }
        row = new List<string>();
    }
    else
    {

        row.Add(cell);
    }
}
public class TableParser
{
    const StringSplitOptions SplitOpts = StringSplitOptions.None;
    const string RowColSep = "|";
    static readonly string[] HeaderColSplit = { "||" };
    static readonly string[] RowColSplit = { RowColSep };
    static readonly string[] MLColSplit = { @"\\" };

    public class TableRow
    {
        public List<string[]> Cells;
    }

    public class Table
    {
        public string[] Header;
        public TableRow[] Rows;
    }

    public static Table Parse(string text)
    {
        // Isolate the header columns and rows remainder.
        var headerSplit = text.Split(HeaderColSplit, SplitOpts);
        Ensure(headerSplit.Length > 1, "At least 1 header column is required in the input");

        // Need to check whether there are any rows.
        var hasRows = headerSplit.Last().IndexOf(RowColSep) >= 0;
        var header = headerSplit.Skip(1)
            .Take(headerSplit.Length - (hasRows ? 2 : 1))
            .Select(c => c.Trim())
            .ToArray();

        if (!hasRows) // If no rows for this table, we are done.
            return new Table() { Header = header, Rows = new TableRow[0] };

        // Get all row columns from the remainder.
        var rowsCols = headerSplit.Last().Split(RowColSplit, SplitOpts);

        // Require same amount of columns for a row as the header.
        Ensure((rowsCols.Length % (header.Length + 1)) == 1, 
            "The number of row colums does not match the number of header columns");
        var rows = new TableRow[(rowsCols.Length - 1) / (header.Length + 1)];

        // Fill rows by sequentially taking # header column cells 
        for (int ri = 0, start = 1; ri < rows.Length; ri++, start += header.Length + 1)
        {
            rows[ri] = new TableRow() { 
                Cells = rowsCols.Skip(start).Take(header.Length)
                    .Select(c => c.Split(MLColSplit, SplitOpts).Select(p => p.Trim()).ToArray())
                    .ToList()
            };
        };

        return new Table { Header = header, Rows = rows };
    }

    private static void Ensure(bool check, string errorMsg)
    {
        if (!check)
            throw new InvalidDataException(errorMsg);
    }
}
public static void Main(params string[] args)
{
        var wikiLine = @"|| Owner|| Action || Status || Comments || | Bill\\ | fix the lobby |In Progress | This is eary| | Joe\\ |fix the bathroom\\ | In progress| plumbing  \\Electric \\Painting \\ \\ | | Scott \\ | fix the roof \\ | Complete | this is expensive|";
        var table = TableParser.Parse(wikiLine);

        Console.WriteLine(string.Join(", ", table.Header));
        foreach (var r in table.Rows)
            Console.WriteLine(string.Join(", ", r.Cells.Select(c => string.Join(Environment.NewLine + "\t# ", c))));
}
var str = @"|| Owner|| Action || Status || Comments || | Bill\\ | fix the lobby |In Progress | This is eary| | Joe\\ |fix the bathroom\\ | In progress| plumbing  \\Electric \\Painting \\ \\ | | Scott \\ | fix the roof \\ | Complete | this is expensive|";

var headerStop = str.LastIndexOf("||");
var headers = str.Substring(0, headerStop).Split(new string[1] { "||" }, StringSplitOptions.None).Skip(1).ToList();
var records = str.Substring(headerStop + 4).TrimEnd(new char[2] { ' ', '|' }).Split(new string[1] { "| |" }, StringSplitOptions.None).ToList();

var tbl = new DataTable();
headers.ForEach(h => tbl.Columns.Add(h.Trim()));
records.ForEach(r =>  tbl.Rows.Add(r.Split('|')));
private void ParseWikiTable(string input, string newLineReplacement = " ")
{
    string separatorHeader = "||";
    string separatorRow = "| |";
    string separatorElement = "|";

    input = Regex.Replace(input, @"[ \\]{2,}", newLineReplacement);

    string inputHeader = input.Substring(0, input.LastIndexOf(separatorHeader));
    string inputContent = input.Substring(input.LastIndexOf(separatorHeader) + separatorHeader.Length);

    string[] headerArray = SimpleSplit(inputHeader, separatorHeader);
    string[][] rowArray = SimpleSplit(inputContent, separatorRow).Select(r => SimpleSplit(r, separatorElement)).ToArray();

    // do something with output data
    TestPrint(headerArray);
    foreach (var r in rowArray) { TestPrint(r); }
}

private string[] SimpleSplit(string input, string separator, bool trimWhitespace = true)
{
    input = input.Trim();
    if (input.StartsWith(separator)) { input = input.Substring(separator.Length); }
    if (input.EndsWith(separator)) { input = input.Substring(0, input.Length - separator.Length); }

    string[] segments = input.Split(new string[] { separator }, StringSplitOptions.None);
    if (trimWhitespace)
    {
        for (int i = 0; i < segments.Length; i++)
        {
            segments[i] = segments[i].Trim();
        }
    }

    return segments;
}

private void TestPrint(string[] lst)
{
    string joined = "[" + String.Join("::", lst) + "]";
    Console.WriteLine(joined);
}
           var text = @"|| Owner|| Action || Status || Comments || | Bill\\ | fix the lobby |In Progress | This is eary| | Joe\\ |fix the bathroom\\ | In progress| plumbing  \\Electric \\Painting \\ \\ | | Scott \\ | fix the roof \\ | Complete | this is expensive|";

        // Get Headers
        var regHeaders = new Regex(@"\|\|\s*(\w[^\|]+)", RegexOptions.Compiled);
        var headers = regHeaders.Matches(text);

        //Get Rows, based on number of headers columns
        var regLinhas = new Regex(String.Format(@"(?:\|\s*(\w[^\|]+)){{{0}}}", headers.Count));
        var rows = regLinhas.Matches(text);

        var tbl = new DataTable();

        foreach (Match header in headers)
        {
            tbl.Columns.Add(header.Groups[1].Value);
        }

        foreach (Match row in rows)
        {
            tbl.Rows.Add(row.Groups[1].Captures.OfType<Capture>().Select(col => col.Value).ToArray());
        }
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace parseWiki
{
    class Program
    {
        static void Main(string[] args)
        {
            string content = @"|| Owner || Action || Status || Comments || | Bill\\ | fix the lobby |In Progress | This is eary| | Joe\\ |fix the bathroom\\ | In progress| plumbing  \\Electric \\Painting \\ \\ | | Scott \\ | fix the roof \\ | Complete | this is expensive|";
            content = content.Replace(@"\\", "");
            string headerContent = content.Substring(0, content.LastIndexOf("||") + 2);
            string cellContent = content.Substring(content.LastIndexOf("||") + 2);
            MatchCollection headerMatches = new Regex(@"\|\|([^|]*)(?=\|\|)", RegexOptions.Singleline).Matches(headerContent);
            MatchCollection cellMatches = new Regex(@"\|([^|]*)(?=\|)", RegexOptions.Singleline).Matches(cellContent);

            List<string> headers = new List<string>();
            foreach (Match match in headerMatches)
            {
                if (match.Groups.Count > 1)
                {
                    headers.Add(match.Groups[1].Value.Trim());
                }
            }

            List<List<string>> body = new List<List<string>>();
            List<string> newRow = new List<string>();
            foreach (Match match in cellMatches)
            {
                if (newRow.Count > 0 && newRow.Count % headers.Count == 0)
                {
                    body.Add(newRow);
                    newRow = new List<string>();
                }
                else
                {
                    newRow.Add(match.Groups[1].Value.Trim());
                }
            }
            body.Add(newRow);

            print(headers, body);
        }

        static void print(List<string> headers, List<List<string>> body)
        {
            var CELL_SIZE = 20;

            for (int i = 0; i < headers.Count; i++)
            {
                Console.Write(headers[i].Truncate(CELL_SIZE).PadRight(CELL_SIZE) + "  ");
            }
            Console.WriteLine("\n" + "".PadRight( (CELL_SIZE + 2) * headers.Count, '-'));

            for (int r = 0; r < body.Count; r++)
            {
                List<string> row = body[r];
                for (int c = 0; c < row.Count; c++)
                {
                    Console.Write(row[c].Truncate(CELL_SIZE).PadRight(CELL_SIZE) + "  ");
                }
                Console.WriteLine("");
            }

            Console.WriteLine("\n\n\n");
            Console.ReadKey(false);
        }
    }

    public static class StringExt
    {
        public static string Truncate(this string value, int maxLength)
        {
            if (string.IsNullOrEmpty(value) || value.Length <= maxLength) return value;
            return value.Substring(0, maxLength - 3) + "...";

        }
    }
}