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

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()) + " ");

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]", "*");
}

Regex to split string into groups [duplicate]

This question already has answers here:
How do I split a string by a multi-character delimiter in C#?
(10 answers)
Closed 4 years ago.
How can I match this string in c# using regex so it returns 4 groups per line?
1 or more digits|one or more letters|one or more letters|one ore more X-Digit(s)\n
Example:
123|ABC|ABC|X-1;X-12;X-13
123|ABC|ABC|X-1
I've tried this
\d+\|(A-Z)\|(A-Z)\|(X-)d+
Why shooting with canons at birds?=! If you could simply use the String.Split method to achieve that
string test = "123|ABC|ABC|X-1;X-12;X-13";
string [] groups = test.Split('|');
it will return an array of elements that were separated by a |

Extract part of the string [duplicate]

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

C# RegEx to create string array split on spaces and phrases (in quotes) [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Regular Expression to split on spaces unless in quotes
I am dealing with various strings that I need to split into an array wherever there is a space, except for if that space exists within "quotes".
So for example, I would like this:
this is "a simple" test
..to become:
[0] = this
[1] = is
[2] = "a simple"
[3] = test
Note I would like to retain the quotes surrounding the phrase, not remove them.
The regex:
".*?"|[^\s]+
Usage:
String input = #"this is ""a simple"" test";
String[] matches =
Regex.Matches(input, #""".*?""|[^\s]+").Cast<Match>().Select(m => m.Value).ToArray();

Categories