Take a word from a file and then the next word. C#

117 Views Asked by At

I need some advice here. I am making an application to get a particular SQL statement within a VB Form but do not know which would be the best option to do so.

For example i have this sentence on to explain:

  FROM sys.master_files WITH (NOLOCK)

But what i need is

 FROM sys.master_files

I need to get the FROM and the next thing like sys.master_files

The first moment this is just what I need, it is a basic application, only to catch the FROM and the next thing that comes.

Now I've been researching and looking at some questions that had already been made in StackOverFlow and I was wondering if the Regex.Match is a faster way to do this check or if I could use the IndexOf but the I did not understand very well how it works.

This is what i made so far on the Code.

private void btnSearch_Click(object sender, EventArgs e)
{
    string value = cboParam.Text;
    string[] reservedWords = { "FROM", "SELECT", "JOIN", "UPDATE", "DROP", "ALTER", "CREATE" };

    if (cboParam.Text == "")
    {
        MessageBox.Show("Wrong try again.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    else
    {
        List<string> listParam = new List<string>();
        listParam.Add(value);
        if (selectedPath == null && openFileDialog.FileNames == null)
        {
            MessageBox.Show("Choose a directory please.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        else if (selectedPath != null)
        {
            foreach (string str in selectedPath)
            {
                string split = " ";
                string readText = File.ReadAllText(str)
                    .Replace("\n", split)
                    .Replace("\r", split)
                    .Replace("_", split)
                    .Replace("-", split)
                    .Replace("SQL", split)
                    .Replace("sql", split)
                    .Replace("FROM", split)
                    .Replace("from", split)
                    .Replace("JOIN", split)
                    .Replace("join", split)
                    .Replace("UPDATE", split)
                    .Replace("update", split)
                    .Replace("DELETE", split)
                    .Replace("delete", split);
                readText.IndexOf(value);

            }
        }
        else if (openFileDialog.FileNames != null)
        {
            foreach (string str in openFileDialog.FileNames)
            {
                string split = "\nGO";
                string readText = File.ReadAllText(str) 
                    .Replace("\n", split)
                    .Replace("\r", split)
                    .Replace("_", split)
                    .Replace("-", split)
                    .Replace("SQL", split)
                    .Replace("sql", split)
                    .Replace("FROM", split)
                    .Replace("from", split)
                    .Replace("JOIN", split)
                    .Replace("join", split)
                    .Replace("UPDATE", split)
                    .Replace("update", split)
                    .Replace("DELETE", split)
                    .Replace("delete", split);

                readText.IndexOf(value);
            }
        }
    }
}

That's what I had, I was trying to use the IndexOf but can not give a continuity because I did not understand the operation of it.

For a better understanding I'm from Brazil so I changed a bit the pair variables of code into English to become "easy" to understand the code.

3

There are 3 best solutions below

0
John On

If you're looking for a regex to get that part:

string s = System.Text.RegularExpressions.Regex.Match("test FROM sys.master_files WITH (NOLOCK)", "FROM ([^ ]*)").Groups[1].Value;

Another solution with the use of Linq:

string xx = "test FROM sys.master_files WITH (NOLOCK)";
string yy = xx.Split(' ').SkipWhile(x => x != "FROM").Take(2).Last();

And i'm sure there's a ton more.

3
Uriil On

That's defenetly task for regular expressions:

var input = "FROM sys.master_files WITH (NOLOCK) FROM sys1.master_files1 WITH (NOLOCK)";
var rg = new Regex(@"FROM [a-zA-Z0-9\._]+ ");
var result = rg.Matches(input);
1
Tyress On

I'm basing my understanding of the question using your title, which says that you need the first two words of the string. Using IndexOf and Substring:

string original = "FROM sys.master_files WITH (NOLOCK)";
string firstTwoWords = original.Substring(0, original.IndexOf(' ', original.IndexOf(' ')+1));

Should get you FROM sys.master_files

What the second line does is that it gets a part of the original string that starts from index 0, and ends with the second occurrence of the space. But this won't work well with multiple spaces in between words (written by accident).

So you can do something like this that takes care of empty spaces:

    string original = "FROM sys.master_files  WITH (NOLOCK)";
    string[] array = original.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries);
    string newString = array[0] +' '+ array[1];