gpt4 book ai didi

c# - 多个正则表达式字符串模式(不同字段)

转载 作者:行者123 更新时间:2023-11-30 17:44:13 24 4
gpt4 key购买 nike

我正在尝试从具有以下格式的 word 文档中提取文本并将数据插入到 SQL 数据库中。

Word 文档

Name of House: Aasleagh Lodge
Townland: Srahatloe
Near: Killary Harbour, Leenane
Status/Public Access: maintained, private fishing lodge
Date Built: 1838-1850, burnt 1923, rebuilt 1928

源代码

        var wordApp = new Microsoft.Office.Interop.Word.Application();
var wordDoc = wordApp.Documents.Open(@"C:\Users\mhoban\Documents\Book.docx");
var txt = wordDoc.Content.Text;

var regex = new Regex(@"(Name of House\: )(.+?)[\r\n]");

var allMatches = regex.Matches(txt);
foreach (Match match in allMatches)
{
var nameValue = match.Groups[2].Value;
var townValue = match.Groups[2].Value;

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
SqlCommand com = new SqlCommand();

com.CommandText = "INSERT INTO Houses (Name, Townland) VALUES (@name, @town)";

com.Parameters.Add("@name", SqlDbType.NVarChar).SqlValue = nameValue;
com.Parameters.Add("@town", SqlDbType.NVarChar).SqlValue = townValue;

com.Connection = con;

con.Open();

com.ExecuteNonQuery();

con.Close();
}

这非常有效,唯一的问题是我将如何编写代码来插入文本的其他字段,例如这一行

var regex = new Regex(@"(Name of House\: )(.+?)[\r\n]");

在本例中插入房屋的名称“Aasleagh Lodge”,但我将如何编写此行以插入城镇?

我尝试用我需要的字段名称替换正则表达式中的“Townland”,但我最终得到的是单个记录,每个记录只包含一个不同的列值。

有没有一种方法可以同时插入数据,也许是通过使用列表或其他方式,这样就不会发生这种情况。

新源代码

var wordApp = new Microsoft.Office.Interop.Word.Application();
var wordDoc = wordApp.Documents.Open(@"C:\Users\mhoban\Documents\Book.docx");
var txt = wordDoc.Content.Text;

using (var sr = new StringReader(txt))
{
var s = string.Empty;
var nameValue = new StringBuilder();
var townValue = new StringBuilder();
while ((s = sr.ReadLine()) != null)
{
if (s.StartsWith("Name of House"))
{
nameValue.Append(s.Split(new[] { ':' })[1].Trim());
}
else if (s.StartsWith("Townland"))
{
townValue.Append(s.Split(new[] { ':' })[1].Trim());
}

if (nameValue.Length > 0 && townValue.Length > 0)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
SqlCommand com = new SqlCommand();

com.CommandText = "INSERT INTO Houses (Name, Townland) VALUES (@name, @town)";
com.CommandText = "INSERT INTO Houses (Name) VALUES (@name)";

com.Parameters.Add("@name", SqlDbType.NVarChar).SqlValue = nameValue;
com.Parameters.Add("@town", SqlDbType.NVarChar).SqlValue = townValue;

com.Connection = con;

con.Open();

com.ExecuteNonQuery();

con.Close();

nameValue.Clear(); townValue.Clear();
}
}
}

数据库字段

[Id]          NCHAR (10)     NULL,
[Name] NVARCHAR (MAX) NULL,
[Townland] NVARCHAR (MAX) NULL,
[Near] NVARCHAR (MAX) NULL,
[Status] NVARCHAR (MAX) NULL,
[Built] NVARCHAR (MAX) NULL,
[Description] NVARCHAR (MAX) NULL,
[Families] NVARCHAR (MAX) NULL,
[Images] IMAGE NULL

最佳答案

这是一个没有正则表达式的解决方案。你真的不需要它。

var txt = "Name of House: Aasleagh Lodge\r\nTownland: Srahatloe\r\nNear: Killary Harbour, Leenane\r\nStatus/Public Access: maintained, private fishing lodge\r\nDate Built: 1838-1850, burnt 1923, rebuilt 1928\r\nName of House: House of Lan\r\nTownland: Another town land\r\nNear: Killary Harbour, Leenane\r\nStatus/Public Access: maintained, private fishing lodge\r\nDate Built: 1838-1850, burnt 1923, rebuilt 1928\r\nName of House: New Lodge\r\nTownland: NewTownLand\r\nNear: Killary Harbour, Leenane\r\nStatus/Public Access: maintained, private fishing lodge\r\nDate Built: 1838-1850, burnt 1923, rebuilt 1928";
using (var sr = new StringReader(txt))
{
var s = string.Empty;
var nameOfHouse = new StringBuilder();
var townland = new StringBuilder();
while ((s = sr.ReadLine()) != null)
{
if (s.StartsWith("Name of House"))
{
nameOfHouse.Append(s.Split(new[] {':'})[1].Trim());
}
else if (s.StartsWith("Townland"))
{
townland.Append(s.Split(new[] { ':' })[1].Trim());
}

if (nameOfHouse.Length > 0 && townland.Length > 0)
{
// INSERT THE VALUES AND RESET THEM
nameOfHouse.Clear(); townland.Clear();
}
}
}

关于c# - 多个正则表达式字符串模式(不同字段),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29939184/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com