I have this string:
"(Id=7) OR (Id=6) OR (Id=8)"
from the string above how can I create array or list like this:
"Id=6"
"Id=7"
"Id=8"
Without using Regex but with some Linq you could write
string test = "(Id=7) OR (Id=6) OR (Id=8)";
var result = test
.Split(new string[] { " OR "}, StringSplitOptions.None)
.Select(x => x = x.Trim('(', ')'))
.ToList();
If you need also to take in consideration the presence of the AND operator or a variable number of spaces between the AND/OR and the conditions then you could change the code to this one
string test = "(Id=7) OR (Id=6) OR (Id=8)";
var result = test
.Split(new string[] { "OR", "AND"}, StringSplitOptions.None)
.Select(x => x = x.Trim('(', ')', ' '))
.ToList();
I suggest combining regex and LINQ powers:
var result = Regex.Matches(input, #"\(([^()]+)\)")
.Cast<Match>()
.Select(p => p.Groups[1].Value)
.ToList();
The \(([^()]+)\) pattern (see its demo) will match all (...) strings and use the Group 1 (inside unescaped (...)) to build the final list.
Simply grab the matches
(?<=\()[^)]*(?=\))
See demo.
https://regex101.com/r/iJ7bT6/18
string strRegex = #"(?<=\()[^)]*(?=\))";
Regex myRegex = new Regex(strRegex, RegexOptions.Multiline);
string strTargetString = #"(Id=7) OR (Id=6) OR (Id=8)";
foreach (Match myMatch in myRegex.Matches(strTargetString))
{
if (myMatch.Success)
{
// Add your code here
}
}
Related
I have a string like this:
john "is my best buddy" and he loves "strawberry juice"
I want to-
Extract texts within double-quotes into a string array array1
Split texts outside of double-quotes by spaces and then insert them into another string array (array2).
Output:
array1[0]: is my best buddy
array1[1]: strawberry juice
array2[0]: john
array2[1]: and
array2[2]: he
array2[3]: loves
Any help is appreciated.
Clearly, this is a call for Regular Expressions:
var str = #"john ""is my best buddy"" and he loves ""strawberry juice""";
var regex = new Regex("(\"(?'quoted'[^\"]+)\")|(?'word'\\w+)",
RegexOptions.Singleline|RegexOptions.Compiled);
var matches = regex.Matches(str);
var quotes = matches.Cast<Match>()
.SelectMany(m => m.Groups.Cast<Group>())
.Where(g => g.Name == "quoted" && g.Success)
.Select(g => g.Value)
.ToArray();
var words = matches.Cast<Match>()
.SelectMany(m => m.Groups.Cast<Group>())
.Where(g => g.Name == "word" && g.Success)
.Select(g => g.Value)
.ToArray();
I have a string which has string values separated by special character ';' and I need to split string and store each value in separate string. In my ipStr, keywords like ServerName, DBName, TableNames and ColumnNames are identifiers and it will not change only the values might get changed.
For Example.
string ipStr = "ServerName=DevTestServer;DBName=CustomerSummary;TableNames=CustomerDetails&OrderDetails;ColumnNames=ID,CustName,OrderID;"
Now I want to split ServerName, DBName, TableNames and ColumnNames values separately and store each value in in different strings. I tried below but after finding ServerName, identifying DBName part looks difficult and also it does not look like a proper way of coding.
string ServerIdentifier = "ServerName=";
string separator = ";";
string serverName = ipStr.Substring(ipStr.IndexOf(ServerIdentifier), ipStr.IndexOf(delimiter));
What is the easiest way of getting values like below from the ipStr.
string ServerName="DevTestServer";
string DBName="CustomerSummary";
string TableNames="CustomerDetails&OrderDetails";
string ColumnNames="ID,CustName,OrderID";
SqlConnectionStringBuilder won't work because ServerName etc isn't a valid token in a connection string.
However, a low tech approach is to use a good old fashioned Split and ToDictionary
var someWeirdStr = "ServerName=DevTestServer;DBName=CustomerSummary;TableNames=CustomerDetails&OrderDetails;ColumnNames=ID,CustName,OrderID;";
var results = someWeirdStr
.Split(';',StringSplitOptions.RemoveEmptyEntries)
.Select(x => x.Split('='))
.ToDictionary(x => x[0], x => x.ElementAtOrDefault(1));
Console.WriteLine(results["ServerName"]);
Console.WriteLine(results["DBName"]);
Console.WriteLine(results["TableNames"]);
Console.WriteLine(results["ColumnNames"]);
Output
DevTestServer
CustomerSummary
CustomerDetails&OrderDetails
ID,CustName,OrderID
you need to split the string by semi colon and then remove any empty strings then, after that you can split again by equals and create a dictionary of the results.
string ipStr = "ServerName=DevTestServer;DBName=CustomerSummary;TableNames=CustomerDetails&OrderDetails;ColumnNames=ID,CustName,OrderID;";
var values = ipStr.Split(';')
.Where(x => !string.IsNullOrEmpty(x))
.Select(x => {
var pair = x.Split('=');
return KeyValuePair.Create<string, string>(pair[0], pair[1]);
})
.ToDictionary(pair => pair.Key, pair => pair.Value);
foreach (var i in values) {
Console.WriteLine($"{i.Key}: {i.Value}");
}
Here is a working demo:
string ipStr = "ServerName=DevTestServer;DBName=CustomerSummary;TableNames=CustomerDetails&OrderDetails;ColumnNames=ID,CustName,OrderID;";
Dictionary<string, string> dict = Regex
.Matches(ipStr, #"\s*(?<key>[^;=]+)\s*=\s*((?<value>[^'][^;]*)|'(?<value>[^']*)')")
.Cast<Match>()
.ToDictionary(m => m.Groups["key"].Value,m => m.Groups["value"].Value);
result:
I have a very long string of text that is many words separated by camelCase like so:
AedeagalAedilityAedoeagiAefaldnessAegeriidaeAeginaAeipathyAeneolithicAeolididaeAeonialAerialityAerinessAerobia
I need to find the most common word and the number of times it has been used, I am unaware how to do this due to the lack of spaces and being new to C#.
I have tried many methods but none seem to work, any advice you have I'd be very grateful.
I have a github repo with the file being downloaded and a few tests already done here: https://github.com/Imstupidpleasehelp/C-code-test
Thank you.
You can try querying the string with a help of regular expressions and Linq:
string source = ...
var result = Regex
.Matches(source, "[A-Z][a-z]*")
.Cast<Match>()
.Select(match => match.Value)
.GroupBy(word => word)
.Select(group => (word : group.Key, count : group.Count()))
.OrderByDescending(pair => pair.count)
.First();
Console.Write($"{result.word} appears {result.count} time");
string[] split = Regex.Split(exampleString, "(?<=[A-Za-z])(?=[A-Z][a-z])");
var result = split.GroupBy(s => s)
.Where(g=> g.Count()>=1 )
.OrderByDescending(g => g.Count())
.Select(g => new{ Word = g.Key, Occurrences = g.Count()});
var result will contain pairs of (Word, Occurrences) for all words.
If you want just the first one (the one with the most occurrences) use
var result = split.GroupBy(s => s)
.Where(g=> g.Count()>=1 )
.OrderByDescending(g => g.Count())
.Select(g => new{ Word = g.Key, Occurrences = g.Count()}).First();
Have in mind that it can happen that you have 2 or more words with the same number of occurrences, so using First() would only give you one of those.
A non-linq approach using for loop and IsUpper to separate the words.
string data = "AedeagalAedilityAedoeagiAefaldness";
var words = new List<string>();
var temp = new StringBuilder();
for(int i = 0;i < data.Length;i++)
{
temp.Append(data[i]);
if (i == data.Length-1 || char.IsUpper(data[i+1]))
{
words.Add(temp.ToString());
temp.Clear();
}
}
I want to extract the strings like aaa.a1 and aaa.a2 from my list. All this strings contain "aaa.".
How can I combine Regex with Linq?
var inputList = new List<string>() { "bbb aaa.a1 bbb", "ccc aaa.a2 ccc" };
var result = inputList.Where(x => x.Contains(#"aaa.")).Select(x => x ???? ).ToList();
You may use
var inputList = new List<string>() { "bbb aaa.a1 bbb", "ccc aaa.a2 ccc" };
var result = inputList
.Select(i => Regex.Match(i, #"\baaa\.\S+")?.Value)
.Where(x => !string.IsNullOrEmpty(x))
.ToList();
foreach (var s in result)
Console.WriteLine(s);
Output:
aaa.a1
aaa.a2
See C# demo
The Regex.Match(i, #"\baaa\.\S+")?.Value part tries to match the following pattern in each item:
\b - a word boundary
aaa\. - an aaa. substring
\S+ - 1+ non-whitespace chars.
The .Where(x => !string.IsNullOrEmpty(x)) will discard empty items that result from the items with no matching strings.
You could try slight different solution:
var result = inputList
.Where(i => Regex.Match(i, #"\baaa\.[a-z0-9]+")?.Success)
// or even
// .Where(i => Regex.Match(i, #"\ba+\.[a-z0-9]+")?.Success)
I would like to use the .net Regex.Split method to split this input string into an array. It must group the word.
Input: **AAA**-1111,**AAA**-666,**SMT**-QWQE,**SMT**-TTTR
Expected output:
**AAA** : 1111,666
**SMT** : QWQE,TTTR
What pattern do I need to use?
As the comment on the question notes, you cannot do this in a single step (regex or not).
So:
Split on commas.
Split on dash (but keep the pairs)
Group by the first part of each pair.
Something like:
var result = select outer in input.Split(",")
let p = outer.Split('-') // will be string[2]
select new { identifier = p[0], value = p[1] }
into pair
group by pair.identifier into g
select new {
identifier = g.Key
values = String.Join(",", g)
}
This should give you an IEnumerable with a key-string and a string listing (separated by comma) the values fore each:
var input = "AAA-1111,AAA-666,SMT-QWQE,SMT-TTTR";
var list = input.Split(',')
.Select(pair => pair.Split('-'))
.GroupBy(pair => pair.First())
.Select(grp =>
new{
key = grp.Key,
items = String.Join(",", grp.Select(x => x[1]))
});
You can then use it for example like this (if you just want to output the values):
string output = "";
foreach(var grp in list)
{
output += grp.key + ": " + grp.items + Environment.NewLine;
}
FWIW here's the same solution in fluent syntax which might be easier to understand:
string input = "AAA-1111,AAA-666,SMT-QWQE,SMT-TTTR";
Dictionary<string, string> output = input.Split(',') // first split by ','
.Select(el => el.Split('-')) // then split each inner element by '-'
.GroupBy(el => el.ElementAt(0), el => el.ElementAt(1)) // group by the part that comes before '-'
.ToDictionary(grp => grp.Key, grp => string.Join(",", grp)); // convert to a dictionary with comma separated values
-
output["AAA"] // 1111,666
output["SMT"] // QWQE,TTTR