Although its easy in pyhton but i am new to C# and i am having trouble extracting a particular word from a string. i have two txt file.
abc.txt
select * from schema1.table1
xyz.txt
select * from schema2.table2 where a=5
i need to extract " schema1 " and " schema2 " words only but i tried but i am having trouble with it as it is C#.
MY code
{
StreamReader sr = new StreamReader(file);
string data = sr.ReadLine();
while (data != null)
{
string[] values = data.Split('.');
foreach (string value in values)
{
Console.WriteLine(value.Split(' ').Last());
data = sr.ReadLine();
}
}
}
but the output gives whole lot of other words too. any kind of lead is appreciated.
You may try the following:
string sql = "select * from schema2.table2 where a=5";
var schema = Regex.Replace(sql, @"^select \* from ([^.]+)\.\S+.*$", "$1");
Console.WriteLine(schema); // schema2
This answer makes very large assumptions, including that every SQL query you would need to parse would always start with select * from some_schema.some_table
. Obviously, for more complex/different queries, the above logic would fail.
In general, you might need to find a .NET library which can parse SQL queries.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.