Extract a character after a particular string [duplicate] - c#

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);
}

Related

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

Use " in regex expression in C# [duplicate]

This question already has answers here:
Escape double quotes in a string
(9 answers)
Closed 5 years ago.
I am trying to get the text inside "" in:
_["Some text to get"]
So I tried the following in C#:
Regex pattern = new Regex(#"_\["(.*?)\"]");
This does not compile because of the " inside the regex expression.
The regex expression seems to work: https://regexr.com/3h076
How to fix this?
Why not just append the inside of the regex to make a larger string
_["Some text to get"]
Regex p = new Regex(#"_\[" + " (.*?)" + "\"]");

Replace a set of characters in a string [duplicate]

This question already has answers here:
RegEx Starts with [ and ending with ]
(4 answers)
Closed 6 years ago.
How to replace a set of characters where I only know the first and the last one, in between is a variable that is not constant.
All I know is that this string will always start with & and it will end with ;
string str = "Hello &145126451; mate!";
How to get rid of &145126451; ?
So the desired result is:
string result = "Hello mate!"
The most easiest way is to use Regex:
Regex yourRegex = new Regex(#"&.*;");
string result = yourRegex.Replace("Hello &145126451; mate!", String.Empty);
Console.WriteLine(result);
Here is a fiddle with example.

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

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