Get text patters from string using regular expression [closed] - c#

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 months ago.
Improve this question
In C# .NET
I have a string with
PSL44T-VK5VF-2B
PSL-44TVK5-VF2B
PS-L44-TVK-5VF-2B
using regular expression can I get the result format as
XXXXXX-XXXX-XX <- PSL44T-VK5VF-2B
PSL-XXXXXX-XXXX <- PSL-44TVK5-VF2B
XX-XXX-XXX-XXX-2B <- PS-L44-TVK-5VF-2B
XX-L44-XXX-XXX-XX <- PS-L44-TVK-5VF-2B

Your question is not clear. But if you want to filter a list of strings with regular expressions, you can do as follows:
List<string> inputs = new List<string>();
inputs.Add("PSL44T-VK5VF-2B");
inputs.Add("PSL-44TVK5-VF2B");
inputs.Add("PS-L44-TVK-5VF-2B");
var myRegex = new Regex("^PSL-.{6}-.{4}$"); // for PSL-XXXXXX-XXXX
List<string> resultList = inputs.Where(f => myRegex.IsMatch(f)).ToList();
and result:

Related

How to split and then join comma separated words into a string to create condition? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have given words like:
general, sports, weather
I want to create a push notification condition like:
condition = "'general' in topics || 'sports' in topics || 'weather' in topics",
How can I do this in one -or two- shot in C# without looping? So I can build a new string and I use it like below:
condition = dynamicCondition,
Try this one:
string input = "general, sports, weather";
string output = string.Join(" || ", input.Split(',').Select(s => $"'{s}' in topics"));
Note: don't forget to using System.Linq

Remove quotes from the start and end of a string [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have the string
"{ "type" : "Stage_FF_Hot_Alerts__c"}"
I want to it be
{ "type" : "Stage_FF_Hot_Alerts__c"}
i.e. I want to remove quotes from start and end. How can I achieve this?
You could use Trim:
var a = "\"{ \"type\" : \"Stage_FF_Hot_Alerts__c\"}\"";
var b = a.Trim('"');
Console.WriteLine(a);
Console.WriteLine(b);
It will remove all leading and trailing quotation marks.
Try it online
You can also use:
var quotedString = "\"hello\"";
var unQuotedString = quotedString.TrimStart('"').TrimEnd('"');

Extract title from name in c# [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
How do I extract title from name as Mr. from Mr.ABC or Dr. from Dr.XYZ or M/S. from M/S. PQR in C#?
I would recommend regex for a clean way to get the title.
Regex regex = new Regex(#"^(Mr|Ms|Dr|Sr)\.");
Match match = regex.Match("Mr.ABC");
Console.WriteLine(match.Value);
You can split the string on '.' and then take the first value.
string str = "Mr.ABC";
string title = str.Spilt('.')[0];
There is no need to use regex.
Use String Split method:
var title = myString.Split('.')[0];

How i can get the first match in regex and import this match in a string? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
For example using a regex expression del(.*?)del get delcatdel
and save the result in string or in text
word1word2delcatdelword3word4deldogdelword5
in my text (or string) i need obtain delcatdel
USING c sharp
please anybody help me
var regex = new Regex("del(.*?)del");
var match = regex.Match("word1word2delcatdelword3word4deldogdelword5");
string matched = match.Value;

how to remove particumar letter combination from a string in c#? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have a string like below
he/a0h/a0dv/a0jks
I would like to be string become as below.
hehdvjks
Need to remove the "/a0" from the string.
The String.Replace method is what you're looking for, just do a;
String myString = "he/a0h/a0dv/a0jks"
myString = myString.Replace("/a0", "")
It'll return a modified 'myString' with all occurrences of the old value ("/a0") replaced with a new value (blank in this case).
The MSDN reference for String.Replace can be found at:
http://msdn.microsoft.com/en-us/library/fk49wtc1%28v=vs.110%29.aspx
var result = "he/a0h/a0dv/a0jks".Replace("/a0", string.Empty);

Categories