how to extract keys from the string below using c#

c# find substring in string
c# string find all occurrences
string substring c#
how to get specific character from string in c#
string split c#
how to get particular text from string in c#
c# regex
substring c# after character

Input query

 select * from mytable where projectname = __$ProjectName$__ and  projectid = __$ProjectId$__ and env = __$EnvType$__

I want list of string (List<string>) as output below.(double underscore+dollar+"string"+dollar+double underscore) Language: C#

 __ $ProjectName$__
 __ $ProjectId$__
 __ $EnvType$__

Try regular expressions; if key

  1. Starts from __$
  2. Contains identifier (which starts from letter A..Z, a..z, can contain letters or\and digits A..Z, a..z, 0..9)
  3. Ends with $__

the pattern to match is __\$[A-Za-z]+[A-Za-z0-9]*\$__

Code:

using System.Text.RegularExpressions;

...

string source = 
  "select * from mytable where projectname = __$ProjectName$__ and  projectid = __$ProjectId$__ and env = __$EnvType$__";

List<string> keys = Regex
  .Matches(source, @"__\$[A-Za-z]+[A-Za-z0-9]*\$__")
  .OfType<Match>()
  .Select(match => match.Value)
  .ToList();

Console.Write(string.Join(Environment.NewLine, keys));

Outcome:

__$ProjectName$__
__$ProjectId$__
__$EnvType$__

String.Substring Method (System), (The table below uses the single \ , which must be escaped as \\ in a C# string). Finding specific text using regular expressions. Pattern, Meaning� Contribute your code and comments through Disqus. Previous: Write a program in C# Sharp to read a string through the keyboard and sort it using bubble sort. Next: Write a C# Sharp program to check whether a given substring is present in the given string

Using Linq:

List<string> output = input.Split(' ').Where(x => x.StartsWith("__$") && x.EndsWith("$__")).ToList();

How to search strings (C# Guide), Learn how to group results using LINQ in C#. By the first letter of a string property. In this case the key is a string , the student's last name. Name starts with G who scored less than 85 Debra Garcia Name starts with M� How to get all keys of a dictionary with C#. The Keys property gets a collection containing the keys in the Dictionary. It returns an object of KeyCollection type.

using System;
using System.Linq;
using System.Text.RegularExpressions;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            var expression = @"select * from mytable where projectname = __$ProjectName$__ and  projectid = __$ProjectId$__ and env = __$EnvType$__";

            var output = new Regex(@"__\$[^\s]+?\$__")
                .Matches(expression)
                .Cast<Match>()
                .Select(m => m.Value)
                .ToList();

            output.ForEach(Console.WriteLine);
        }
    }
}

Group query results (LINQ in C#), Learn about pattern matching expressions in C#. Then, when those statements match, you extract and use information from that value. It was further limited to numeric types and the string type. Each case is evaluated and the code beneath the condition that matches the input variable is executed. In C# and .NET, a string is represented by the String class. The String. Substring method retrieves a substring from a string instance in C#. The method has the following two overloaded forms. Substring (Int32) - Retrieves a substring from the specified position to the end of the string.

Pattern Matching, It implements a hash table with the key and the value strongly typed to be strings rather than objects. Below given are some examples to� If you want a sequence of int, then use a vector<int>. Using the key_char string, the values of the chars in it will serve as the initial value of the ints. std::vector<int> key_num(key_char.begin(), key_char.end()); Then, iterate over each character of key_num and convert it to the equivalent int value for

C#, String. Exception: This method can arise ArgumentOutOfRangeException in two conditions: if the parameters startIndex or length is less than zero� We invoke Substring to extract parts of a string into a new string. We use 1 or 2 parameters—the first is the start index, and the second is the desired length. Part A: We call Substring with just the starting index of the substring. Strings are indexed with the first character 0.

C#, It can also be initialized using collecton initializer syntax with keys and values as shown below. Example: Dictionary Initialization. IDictionary<int, string> dict = new � Specifies key codes and modifiers. Examples. The following code example uses the KeyDown event to determine the type of character entered into the control. // Boolean flag used to determine when a character other than a number is entered. private: bool nonNumberEntered; // Handle the KeyDown event to determine the type of character entered into the control.

Comments
  • You can use regular expression, something like (__\$[^\$]+\$__) and get all matches with Regex class
  • Use string join "&__&" Then add to beginning "$" and to end "&".
  • Be careful with splitting by space: if query is ...where projectname=__$ProjectName$__ and ... (please, notice abscence of space after =) the __$ProjectName$__ key will not be extracted
  • Your regex may match empty strings and blank strings