How to parse string "\"bcd ef\" a 'x y'", and catch all text between quotes ",' and without them with regular expressions? I tried pattern "(\\\"|')(.*?)(\\\"|'), but got only "bcd ef", 'x y'. Result should be:
"bcd ef"
a
'x y'
string pattern ="(\\\"|')(.*?)(\\\"|')";
Regex regex = new Regex(pattern);
Two options are string.Split() or Regex.Split(). string.Split() is much faster but Regex.Split() is more powerful.
string.Split() version:
var parts = input.Split(new []{'"', '\''})
.Where(p => !string.IsNullOrEmpty(p))
.Select(p => p.Trim())
.ToList();
Regex.Split()version:
var input = "\"bcd ef\" a 'x y'";
var parts = Regex.Split(input, "[\"']")
.Where(p => !string.IsNullOrEmpty(p))
.Select(p => p.Trim())
.ToList();
As long as you want to split by single characters, the regex version is simply slower. So there's no reason to use it.
Docs:
https://learn.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.regex.split
https://learn.microsoft.com/en-us/dotnet/api/system.string.split
Related
I am getting crazy on this! I would like to extract every part of a string that is surrounded by double curly brackets but is not inside a single quotation.
So when "{{Test}} '{{hello}}' {{what}} {that}}"
The result would be:
{{Test}}
{{what}}
Update:
With this I am getting all parts that are surrounded by double curly brackets:
Regex.Matches(input, #"(\{\{.+?\}\})")
Update 2:
Not realy beautifull but I think this works:
var removedQuotationParts = Regex.Replace(input, #"(['].+?['])", "");
var parts = Regex.Matches(removedQuotationParts, #"(\{\{.+?\}\})")
.Cast<Match>()
.Select(m => m.Value)
.ToList();
You may use the Best Trick Ever, namely, match any text inside apostrophes, and then match and capture your expected pattern.
A possible regex will look like
'[^']*'|({{.*?}})
It matches
'[^']*' - ', then any 0+ chars other than ' and then a '
| - or
({{.*?}}) - Capturing group 1: {{, any 0+ chars, as few as possible (including newlines due to RegexOptions.Singleline option, see code below), then }}.
See the C# demo:
var input = "{{Test}} '{{hello}}' {{what}} {that}}";
var result = Regex.Matches(input, #"'[^']*'|({{.*?}})", RegexOptions.Singleline)
.Cast<Match>()
.Select(m => m.Groups[1].Value)
.Where(x => !String.IsNullOrEmpty(x))
.ToList();
foreach (var s in result)
Console.WriteLine(s);
Output:
{{Test}}
{{what}}
NOTE: .Where(x => !String.IsNullOrEmpty(x)) is necessary to omit all those matches where Group 1 did not match.
I am having 1 long string in which i want to find word starting with Emp only after dot in my string and if match then extract that part after dot.
Below is my string:
Value.EmployeeRFID,Value.EmployeeRFID1,Value.EmkhasisGFTR,Value.EmployeeGHID,Value.EmployeeFCKJ
Now in my above input i just want to extract only part after dot(Foreg:EmployeeRFID,EmployeeRFID1 etc) and i want to add that in below list:
var list= new List<string>();
Expected output in above list variable:
[0]:EmployeeRFID ,[1]=EmployeeRFID1, [2]:EmployeeGHID, [3]:EmployeeFCKJ
This is how i am trying with linq but i am getting Enumeration yielded no results:
string str="Value.EmployeeRFID,Value.EmployeeRFID1,Value.EmkhasisGFTR,Value.EmployeeGHID,Value.EmployeeFCKJ";
var data= str.Where(t=>t.ToString().StartsWith("Emp")).Select(t=>t.t.ToString()) // Enumeration yielded no results
Try regular expressions:
string source = "Value.EmployeeRFID,...,Value.EmployeeGHID,Value.EmployeeFCKJ";
string pattern = #"(?<=\.)Emp\w*";
string[] result = Regex.Matches(source, pattern, RegexOptions.IgnoreCase)
.OfType<Match>()
.Select(match => match.Value)
.ToArray();
Test:
// EmployeeRFID
// EmployeeRFID1
// EmployeeGHID
// EmployeeFCKJ
Console.Write(string.Join(Environment.NewLine, result));
string as an argument to LINQ extensions is an IEnumerable<char>, so your t is only one character. You probably meant to do something like this:
var data= str.Split('.')
.Where(t => t.StartsWith("Emp")).Select(t => t.Split(',').First())
.ToList();
But regular expressions as suggested by Dmitry seem to be a better approach for string parsing than LINQ.
I'm trying to find a way to split a string by its letters and numbers but I've had luck.
An example:
I have a string "AAAA000343BBB343"
I am either needing to split it into 2 values "AAAA000343" and "BBB343" or into 4 "AAAA" "000343" "BBB" "343"
Any help would be much appreciated
Thanks
Here is a RegEx approach to split your string into 4 values
string input = "AAAA000343BBB343";
string[] result = Regex.Matches(input, #"[a-zA-Z]+|\d+")
.Cast<Match>()
.Select(x => x.Value)
.ToArray(); //"AAAA" "000343" "BBB" "343"
So you can use regex
For
"AAAA000343" and "BBB343"
var regex = new Regex(#"[a-zA-Z]+\d+");
var result = regex
.Matches("AAAA000343BBB343")
.Cast<Match>()
.Select(x => x.Value);
// result outputs: "AAAA000343" and "BBB343"
For
4 "AAAA" "000343" "BBB" "343"
See #fubo answer
Try this:
var numAlpha = new Regex("(?<Alpha>[a-zA-Z]*)(?<Numeric>[0-9]*)");
var match = numAlpha.Match("codename123");
var Character = match.Groups["Alpha"].Value;
var Integer = match.Groups["Numeric"].Value;
Input: ((Why,Heck),(Ask,Me),(Bla,No))
How can I split this data into a string array:
Element1 (Why,Heck)
Element2 (Ask,Me)
Element3 (Bla,No)
I tried the String.Split or String.TrimEnd/Start but no chance the result is always wrong.
Would it be better with Regex?
var input = "((Why,Heck),(Ask,Me),(Bla,No))";
var result = Regex.Matches(input, #"\([^\(\)]+?\)")
.Cast<Match>()
.Select(m => m.Value)
.ToList();
Another - non regex approach which should work:
string[] result = str.Split(new[]{"),"}, StringSplitOptions.None)
.Select(s => string.Format("({0})", s.Trim('(', ')')))
.ToArray();
Demo
you could also:
remove all parenthesis to simplify your splits
split by ','
Read your returned array in groups of two. That's using a for loop or a similar recursive algorithm, get indices 0 and 1, 2 and 3 e.t.c
Reconstruct with parenthesis
Or you could just use Regular expressions
I have a string like this:
numbers(23,54)
The input format is like this:
numbers([integer1],[integer2])
How can I get the number "23" and "54" using regular expression ? Or are there any other better ways to get?
You can avoid regular expressions usage thus your input has consistent format:
string input = "numbers(23,54)";
var numbers = input.Replace("numbers(", "")
.Replace(")", "")
.Split(',')
.Select(s => Int32.Parse(s));
Or even (if you don't afraid of magic numbers):
input.Substring(8, input.Length - 9).Split(',').Select(s => Int32.Parse(s))
UPDATE Here also Regex version
var numbers = Regex.Matches(input, #"\d+")
.Cast<Match>()
.Select(m => Int32.Parse(m.Value));
Yeah Use (\d+) to get the numbers correctly
This is the correct way