Extract part of the string [duplicate] - c#

This question already has answers here:
How do I split a string by a multi-character delimiter in C#?
(10 answers)
Closed 7 years ago.
I have thousands of lines of string type data, and what I need is to extract the string after AS. For example this line:
CASE END AS NoHearing,
What I want would be NoHearing,
This line:
CASE 19083812 END AS NoRequset
What I need would be NoRequset
So far I have tried couple ways of doing it but no success. Can't use .split because AS is not Char type.

If this will be the only way that AS appears in the string:
noRequestString = myString.Substring(myString.IndexOf("AS") + 3);

Using Regex I extract all between the AS and a comma:
string data = #"
CASE END AS NoHearing,
CASE 19083812 END AS NoRequset
";
var items = Regex.Matches(data, #"(?:AS\s+)(?<AsWhat>[^\s,]+)")
.OfType<Match>()
.Select (mt => mt.Groups["AsWhat"])
.ToList();
Console.WriteLine (string.Join(" ", items)); // NoHearing NoRequset

Related

Extract a character after a particular string [duplicate]

This question already has answers here:
Regex for numbers after a certain string part
(2 answers)
Closed 4 years ago.
I want to extract a character after a particular string, example:
Sentence: "Develop1 Tester2 BA3"
String: Develop
Expected result: "1"
I tried the Regex as following but still not get result as my expectation, please consult me, thank in advance.
RegEx: /[DEVELOP\d]\[+-]?\d+(?:\.\d*)?/
You don't really need a regex for this.
Something like this should do the trick:
var input = "Develop1 Tester2 BA3";
var search = "Develop";
if (input.StartsWith(search) && input.Length > search.Length) {
var result = input[search.Length];
Console.Write("Result: " + result);
}

Replace RX123456789 with RX********* using REGEX [duplicate]

This question already has answers here:
mask all digits except first 6 and last 4 digits of a string( length varies )
(13 answers)
Closed 4 years ago.
I'm using C# to create a pattern in order to replace all the occurrences in a string from
RX123456789 into RX*********
I have tried various patterns without success. I'm kinda new regarding Regular Expression.
I appreciate your help.
Use this Regex:-
string data = "RX123456789";
var resultString = Regex.Replace(data, #"[0-9]", "*");
If you need * of number only When RX is present then use this logic:-
string data = "RX123456789";
var resultString="";
if (new Regex("RX([0-9]+)").IsMatch(data))
{
resultString = Regex.Replace(data, #"[0-9]", "*");
}

How to sanitize a string removing specific character? [duplicate]

This question already has answers here:
Is there a simple way to remove multiple spaces in a string?
(26 answers)
How do I replace multiple spaces with a single space in C#?
(28 answers)
Closed 4 years ago.
I have this string:
PO Box 162, Ferny Hills
QLD 4055
Brisbane
which contains these character:
I want remove this charactes, so I tried:
info.Address = dd[i].InnerText
.Replace("\n", " ")
.Replace(" ", "")
.Replace(",", ", ");
but didn't works, I get all the character of the string attached. I'm expecting this result: PO Box 162, Ferny Hills QLD 4055 Brisbane.
Well, you replaced all blanks by nothing ( .Replace(" ", "") ). What did you expect? Now all your blanks are gone.
If you don't want that, don't do it.
you can try like this, by splitting and trimming the string to remove spaces and join them back by newline
var address = dd[i].InnerText.Split(new[] { Environment.NewLine })
.Select(s => s.Trim());
info.Address = String.Join(Environment.NewLine, address);

C# - Split string into an array of string using RegEx and create a dictionary<string,string> [duplicate]

This question already has answers here:
Splitting a string in C#
(4 answers)
Closed 7 years ago.
How can i create a RegEx that will split a string format just like below into an array of string?
The string should have key and value seperated with semicolon, if there is no ',' that seperate the key-value, it should not have pass the test RegEx.
The string will look like this:
var splitMe = "[Key1,Value1][Key2,Value2][Key3,Value3][Key4,Value4]";
var splitedArray = Regex.Split(/'RegEx Here'/);
//Output value should like this one ["Key1,Value1","Key2,Value2","Key3,Value3","Key4,Value4"]
//this value also will be the key and value of a Dictionary<string,string>
It would be simpler to use Regex.Matches chained with Linq to get the dictionary directly :
var input = "[Key1,Value1][Key2,Value2][Key3,Value3][Key4,Value4]";
var dictionary = Regex.Matches(input, #"\[(?<key>\w+),(?<value>\w+)\]")
.Cast<Match>()
.ToDictionary(x => x.Groups["key"].Value, x => x.Groups["value"].Value);
This would work for you, I would imagine:
\[(\w+,\w+)\]
Regex101
I'm not sure what data you might have in keys and values but perhaps the following would be more inclusive?
\[([^,]+?,[^,]+?)\]
If you want to use split still you can match what you need with this:
(\[|\]\[|\])

Split a Pascal-case string into logical set of words [duplicate]

This question already has answers here:
Closed 14 years ago.
I would like to take a pascal-cased string like "CountOfWidgets" and convert it into something more user-friendly like "Count of Widgets" in C#. Multiple adjacent uppercase characters should be left intact. What is the most efficient way to do this?
NOTE: Duplicate of .NET - How can you split a "caps" delimited string into an array?
Don't know about efficient but at least it's terse:
Regex r = new Regex("([A-Z]+[a-z]+)");
string result = r.Replace("CountOfWidgets", m => (m.Value.Length > 3 ? m.Value : m.Value.ToLower()) + " ");

Categories