I have simple sql-query, for example string sql="select 'a1,a2' f1, 2 f2;"
. How to parse string
to object[]
(using sql-syntax rules) without database? May be regex transform can be used.
Asked
Active
Viewed 199 times
2

nick_n_a
- 198
- 15
-
@flipchart Not receive, parse simple without sql-server. – nick_n_a Nov 09 '16 at 10:24
-
Is 'a1,a2' a value of the one column? – Farid Imranov Nov 09 '16 at 10:27
-
@Farid Imranov yes, I could be *split(",")* use, but split obtain wrong result. – nick_n_a Nov 09 '16 at 10:30
-
What result you was receive? – Farid Imranov Nov 09 '16 at 10:31
-
What is desirable result you want to get from this example? – Konstantin Ershov Nov 09 '16 at 11:14
-
Are you looking for how to write a SQL parser in c#, as in the question [Parsing SQL code in C#](https://stackoverflow.com/questions/589096/parsing-sql-code-in-c-sharp)? Because that's too broad of a topic for a single stackoverflow question. – dbc Nov 09 '16 at 11:52
2 Answers
1
var str = "select 'a1,a2' f1, 2 f2;";
Regex.Matches(str, "(^|[\\s,\\,]){1}(\\'.*\\'\\s*\\w+|[^\\,^']*)[\\,,$,;]")
.Cast<Match>()
.Select(m => m.Value.Split(new[] { ' '}, StringSplitOptions.RemoveEmptyEntries)
.First().Replace("\'",""))
.ToArray();

Konstantin Ershov
- 730
- 4
- 13
-
Thanks, I try `select '1,a2' f1,2 a,'123' a,4, 1` or `select 1 a, 2 b, 3 c` and it's fail, but idea good. I use regex `"\\,(('[^']*[^,]*')|[^,]*)" ` and replace *select* to ','. More: `string str = "select '1,a2' f1,2 a,'123' a,4, a"; foreach (Match m in Regex.Matches(str.Replace("select",","),"\\,(('[^']*[^,]*')|[^,]*)")) Console.WriteLine(m.Value.Substring(1));` Thanks. – nick_n_a Nov 09 '16 at 13:00
0
You can use Split
method of string in C#
like .Split(',')

Farid Imranov
- 2,017
- 1
- 19
- 31
-
Your idea is good, but Split gets [a1'][a2'][2] and I need to receive [a1,a2][2] – nick_n_a Nov 09 '16 at 10:31
-
-
Without database use. I want convert sql-query without table to array. – nick_n_a Nov 09 '16 at 10:40
-
then you can use `loop` through the string, find `,` character and if this character is not first use in string then `substring` since to this charcter – Farid Imranov Nov 09 '16 at 10:48