I have pattern string:"Hello {Name}, welcome to {Country}"
and a full value string:"Hello Scott, welcome to VietNam"
How can I extract value of {Name} and {Country}:
Name = Scott, Country = VietNam
I have see some regular expression to resolve this problem but can I apply fuzzy matching here? e.g. With invert string "welcome to VietNam, Hello Scott", we must change the regular expression too?
You can use Regex:
var Matches = Regex.Matches(input, #"hello\s+?([^\s]*)\s*|welcome\s+?to\s+?([^\s]*)", RegexOptions.IgnoreCase);
string Name = Matches.Groups[1].Value;
string Country = Matches.Groups[2].Value;
Update: Changed code to work either way. Demo.
As a more general solution, you can do something like the following:
public Dictionary<string, string> GetMatches(string pattern, string source)
{
var tokens = new List<string>();
var matches = new Dictionary<string, string>();
pattern = Regex.Escape(pattern);
pattern = Regex.Replace(pattern, #"\\{.*?}", (match) =>
{
var name = match.Value.Substring(2, match.Value.Length - 3);
tokens.add(name);
return $"(?<{name}>.*)";
});
var sourceMatches = Regex.Matches(source, pattern);
foreach (var name in tokens)
{
matches[name] = sourceMatches[0].Groups[name].Value;
}
return matches;
}
The method extracts the token names from the pattern, then replaces the tokens with the equivalent syntax for a regular expression named capture group. Next, it uses the modified pattern as a regular expression to extract the values from the source string. Finally, it uses the captured token names with the named capture groups to build a dictionary to be returned.
Just quick and dirty..
string pattern = "Hello Scott, welcome to VietNam";
var splitsArray = pattern.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
var Name = splitsArray[1].Replace(",", string.Empty);
var country = splitsArray[4];
Related
I have a list Textlist1 that contains only strings. Each item in the list begins with the same text: "sentence.text.", but I only want to store the second part of the string in another list, I don't want to store the first part "sentence.text." in the list Textlist2.
For example:
List<string> Textlist1 = new List<string>();
List<string> Textlist2 = new List<string>();
The full strings are stored in Textlist1.
Textlist1[0] = "sentence.text.My name is Fred"
Textlist1[1] = "sentence.text.Jenny is my sister"
How can I only add "My name is Fred" and "Jenny is my sister" to Textlist2?
The result should be like this:
Textlist2[0] = "My name is Fred"
Textlist2[1] = "Jenny is my sister"
See online result: https://dotnetfiddle.net/MpswLZ
List<string> Textlist1 = new List<string>() {
"sentence.text.My name is Fred",
"sentence.text.Jenny is my sister"
};
List<string> Textlist2 = new List<string>();
Textlist2 = Textlist1.Select(item => item.Split('.')[2]).ToList();
You can use Linq. (You need do add using System.Linq)
Textlist2= Textlist1
.Select(i=> i.Substring("sentence.text.".Length))
.ToList();
Split the input strings by the periods, limiting the split to 3. Then take the last entry from the array that split produces.
Textlist2[0] = Textlist1[0].Split('.', 3)[2];
Textlist2[1] = Textlist1[1].Split('.', 3)[2];
You could use Regex.Replace the text.
var regex = new Regex("^sentence.text.",RegexOptions.Compiled);
Textlist2.AddRange(Textlist1.Select(x=>regex.Replace(x,string.Empty)));
The "^" in Regex ensure the required text ("sentence.text") is matched only at the beginning of string and not else where.
Sample Input
Sample Output
List<string> Textlist1 = new List<string>
{
"sentence.text.a",
"sentence.text.b"
};
string prefix = "sentence.text.";
List<string> Textlist2 = Textlist1.Select(x => x.Substring(prefix.Length)).ToList();
https://dotnetfiddle.net/oFhvfs
There are ultimately many ways of doing this, this is just one solution. Which approach you choose depends on how complex the pattern replacement is. I've gone with a simplistic approach as your example is a very simple case.
I have a List contain some strings inside like this and other data.
HwndWrapper[App.exe;;cda6c3f4-8c87-4b12-8f3d-5322ca90eeex]
HwndWrapper[App.exe;;cadac3f4-8c87-4b12-8q3d-1qwe2ca90eec]
HwndWrapper[App.exe;;c1b6a3s4-8c87-4b12-8f3d-2qw2ca90eeev]
My list:
// Returns a list of WindowInformation objects with Handle, Caption, Class,
// Parent, Children, Siblings and process information
List<WindowInformation> windowListExtended = WindowList.GetAllWindowsExtendedInfo();
The regular expresion to match is:
HwndWrapper\[App.exe;;.*?\]
Now for every match on the list. I need extract the string matched and run a process with every string extracted, Foreach or something like that.
Some help please.
Update:
Thanks Altaris for the help, just need convert List to string
var message = string.Join(",", windowListExtended);
string pattern = #"HwndWrapper\[LogiOverlay.exe;;.*?]";
MatchCollection matches = Regex.Matches(message, pattern);
From what I understand you want to extract every match in a separate list to work with, there you go:
var someList = new List<string>{"HwndWrapper[App.exe;;cda6c3f4-8c87-4b12-8f3d-5322ca90eeex]",
"HwndWrapper[App.exe;;cadac3f4-8c87-4b12-8q3d-1qwe2ca90eec]",
"HwndWrapper[App.exe;;c1b6a3s4-8c87-4b12-8f3d-2qw2ca90eeev]"};
Regex FindHwndWrapper = new Regex(#"HwndWrapper\[App.exe;;(.*)\]");
var matches = someList.Where(s => FindHwndWrapper.IsMatch(s)).ToList();
foreach(var match in matches)
{
Console.WriteLine(match);// Use values
}
I used System.Linq function Where() to iterate through list
Use this Linq line if you want just the id parts, like "cda6c3f4-8c87-4b12-8f3d-5322ca90eeex"
var matches = someList.Select(s => FindHwndWrapper.Match(s).Groups[1]).ToList();
I am unsure of what you want exactly, I think you want to extract these
List<string> windowListExtended = new List<string>();
windowListExtended.Add("HwndWrapper[App.exe;;cda6c3f4-8c87-4b12-8f3d-5322ca90eeex]");
windowListExtended.Add("HwndWrapper[App.exe;;cadac3f4-8c87-4b12-8q3d-1qwe2ca90eec]");
windowListExtended.Add("HwndWrapper[App.exe;;c1b6a3s4-8c87-4b12-8f3d-2qw2ca90eeev]");
var myRegex = new Regex(#"HwndWrapper\[App.exe;;.*?]");
var resultList = files.Where(x => myRegex.IsMatch(x)).Select(x => x.Split(new[] { ";;","]" }, StringSplitOptions.None)[1]).ToList();
//Now resultList contains => cda6c3f4-8c87-4b12-8f3d-5322ca90eeex, cadac3f4-8c87-4b12-8q3d-1qwe2ca90eec, c1b6a3s4-8c87-4b12-8f3d-2qw2ca90eeev
foreach (var item in resultList)
{
//Do whatever you want
}
I have a string something like this:
<BU Name="xyz" SerialNo="3838383" impression="jdhfl87lkjh8937ljk" />
I want to extract values like this:
Name = xyz
SerialNo = 3838383
impression = jdhfl87lkjh8937ljk
How to get these values in C#?
I am using C# 3.5.
If by some reason you don't want to use Xml parser you can use reqular expression to achieve this.
Use this regular expression:
(\w)+=\"(\w)+\"
Use this regular expression like this:
var input = #"<BU Name=""xyz"" SerialNo=""3838383"" impression=""jdhfl87lkjh8937ljk"" />";
var pattern = #"(\w)+=\""(\w)+\""";
var result = Regex.Matches(input, pattern);
foreach (var match in result.Cast<Match>())
{
Console.WriteLine(match.Value);
}
Result:
//Name="xyz"
//SerialNo="3838383"
//impression="jdhfl87lkjh8937ljk"
//Press any key to continue.
My string is like
COMMAND="HELP ME" TIMEOUT_SECONDS="30" APP_ID="SOMETHING RANDOM" COUNT="100" RETVAL="0" STDOUT="DATA I NEED" STDERR="NO ERROR" STATUS="SUCCESS"
I want to be able to extract STDOUT, STDERR and STATUS. How can I do it ?
You can try this regex:
(?<=(?:STDOUT|STDERR|STATUS)\=")([^"]+)
As a result you will get 3 results.
MatchCollection mcol = Regex.Matches(strInput, #“(?<=(?:STDOUT|STDERR|STATUS)\=")([^"]+)”);
foreach(Match m in mcol)
{
System.Diagnostic.Debug.Print(m.ToString());
}
Also:
using System.Text.RegularExpressions;
Live Demo
Here, in this part of regex:
(?:STDOUT|STDERR|STATUS)
You can also specify the key (other than the 3 mentioned) whose value is needed.
string input2 = #"COMMAND=""HELP ME"" TIMEOUT_SECONDS=""30"" APP_ID=""SOMETHING RANDOM"" COUNT=""100"" RETVAL=""0"" STDOUT=""DATA I NEED"" STDERR=""NO ERROR"" STATUS=""SUCCESS""";
var dict = Regex.Matches(input2, #"(.+?)=""(.+?)""").Cast<Match>()
.ToDictionary(m => m.Groups[1].Value.Trim(),
m => m.Groups[2].Value.Trim());
Console.WriteLine(dict["STDOUT"]);
Console.WriteLine(dict["STATUS"]);
Use the following function:
public static Dictionary<string,string> GetValues(string command)
{
Dictionary<string,string> output = new Dictionary<string,string>();
string[] splitCommand = command.Split(" ");
foreach(var item in splitCommand)
{
output.Add(item.Split("=")[0] , item.Split("=")[1]);
}
return output;
}
When you want to get the values use the function like
Dictionary<string,string> output = YourClass.GetValue(command);
string stdout = output["STDOUT"];
string etderr= output["ETDERR"];
string status = output["STATUS"];
I donot have access to compiler. So, there might be an error. But the overall functionality will look something like this.
How can I extract the substring "John Woo" from the below string in C#
CN=John Woo,OU=IT,OU=HO,DC=ABC,DC=com
Thanks !
You could use a Lookup<TKey, TElement>:
string text = "CN=John Woo,OU=IT,OU=HO,DC=ABC,DC=com";
var keyValues = text.Split(',')
.Select(s => s.Split('='))
.ToLookup(kv => kv[0], kv => kv.Last());
string cn = keyValues["CN"].FirstOrDefault(); // John Woo
// or, if multiple values with the same key are allowed (as suggested in the given string)
string dc = string.Join(",", keyValues["DC"]); // ABC,com
Note that you neither get an exception if the key is not present(as in a dictionary) nor if the key is not uniqe (as in a dictionary). The value is a IEnumerable<TElement>.
Try this
var regex = new Regex("CN=(?<mygroup>.*?),");
var match = regex.Match("CN=John Woo,OU=IT,OU=HO,DC=ABC,DC=com");
if(match.Success)
{
string result = match.Groups["mygroup"].Value;
}
Try this (this is a non generic answer) :
var name = str.Split(',').Where(n => n.StartsWith("CN=")).FirstOrDefault().Substring(3);
Something like this
var s = "CN=John Woo,OU=IT,OU=HO,DC=ABC,DC=com";
// this give you a enumarable of anonymous key/value
var v = s.Split(',')
.Select(x => x.Split('='))
.Select(x => new
{
key = x[0],
value = x[1],
});
var name = v.First().value; // John Woo
You can firstly split the string by the commas to get an array of strings, each of which is a name/value pair separated by =:
string input = "CN=John Woo,OU=IT,OU=HO,DC=ABC,DC=com";
var nameValuePairs = input.Split(new[] {','});
Then you can split the first name/value pair like so:
var nameValuePair = nameValuePairs[0].Split(new[]{'='});
Finally, the value part will be nameValuePair[1]:
var value = nameValuePair[1];
(No error handling shown above - you would of course have to add some.)
I created the below code of my own and finally got the substring I needed. The below code works for every substring that I want to extract that falls after "CN=" and before first occurrence of ",".
string name = "CN=John Woo,OU=IT,OU=HO,DC=ABC,DC=com";
int index1 = name.IndexOf("=") + 1;
int index2 = name.IndexOf(",") - 3;
string managerName = name.Substring(index1, index2);
The Result was "John Woo"
Thanks all for your help...