c# - Multiple Regex String Patterns (Different Fields) -


i trying extract text word document following format , inserting data sql database.

word document

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 

source code

        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 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();         } 

this works thing how write code insert other fields of text example line

var regex = new regex(@"(name of house\: )(.+?)[\r\n]"); 

inserts name of house in case "aasleagh lodge" how write line insert townland?

i tried replacing "townland" in regex field name require end singular records each holding 1 different column value.

is there way insert data @ same time maybe using list or not occur.

new source code

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 houses (name, townland) values (@name, @town)";                         com.commandtext = "insert 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();                     }                 }             } 

database fields

[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 

here solution without regex. not need here.

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: 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 values , reset them           nameofhouse.clear(); townland.clear();       }    } } 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -