如何从文本文件中提取单词

时间:2017-03-08 11:04:53

标签: c# .net

我有一个商店程序文本文件。我有很多表和列名。

如何从文本文件中获取表名和列名。

例如

select t1.name, t2.roleid, t3.department, t4.country from dbo.user t1 inner join
dbo.role t2 on t1.roleid = t2.roleid inner join dbo.tbldepartment t3 on t1.departmentid= t3.departmentid inner join dbo.country t4 on t1.countryid = t4.countryid

结果

Table name => dbo.user
Coulmn name => name

table name => dbo.role
column name =>roleid

Table name => dbo.tbldepartment
column name => departmentid

Table name =>dbo.country
column name => countryid

1 个答案:

答案 0 :(得分:-1)

一些代码......

$categories = Category->with('posts')
->has('post')
->where('user_id' => $user->id)
->get();

你会得到这样的输出

enter image description here

您可以从此链接https://www.dotnetperls.com/between-before-after

获取您的扩展程序类 SubstringExtensions

同时将此课程添加到项目

string file = File.ReadAllText(@"E:\Projects\testApp\testApp\bin\Debug\file.txt");
string textAfterFrom = file.After(" from ");
string textBeforeFrom = file.Before(" from ");
string[] textArrayAfterSelect = textBeforeFrom.After("select").Split(',');

List<ColumnTableRelation> ListofTable = new List<ColumnTableRelation>();
ColumnTableRelation objRel = new ColumnTableRelation();

string[] splitTables = textAfterFrom.Split('\n');

foreach (string tableNames in splitTables)
{
   objRel = new ColumnTableRelation();
   if (!string.IsNullOrEmpty(tableNames) & !tableNames.Equals("\r"))
   {
       if (tableNames.Contains("dbo."))
       {
          if (tableNames.Contains("inner join"))
          {
            objRel.Tablename = tableNames.Between("inner join", " on ").Trim();
          }
          else
          {
            objRel.Tablename = tableNames.Before("\r").Trim();
          }
       }
       ListofTable.Add(objRel);
   }
}

foreach (var item in ListofTable)
{
   string[] stringArray = textArrayAfterSelect;
   string value = item.Tablename.After(" ");
   var matchingvalues = stringArray.Where(stringToCheck => stringToCheck.Contains(value));
   List<string> listString = new List<string>();
   foreach (var match in matchingvalues)
   {
      listString.Add(match);
   }
   item.ColumnNames = listString;
}
StringBuilder builder = new StringBuilder();
foreach (var item in ListofTable)
{
   builder.Append(Environment.NewLine);
   builder.Append("Table name => " + item.Tablename + Environment.NewLine);
   foreach (var columns in item.ColumnNames)
   {
      builder.Append("Column name => " + columns + Environment.NewLine);
   }
}
string yourResult = builder.ToString();
MessageBox.Show(yourResult);

任何查询都将受到赞赏

我用

尝试了这个
public class ColumnTableRelation
{
    public string Tablename { get; set; }
    public List<string> ColumnNames { get; set; }
}