Regex to match pattern in C# - c#

I have a string in the following format
ABC=23:Qasd=56:Def=40.44
I would like to replace all the strings (ABC=, Qasd= and Def=) with empty string. The string after = can be anything. So my output string would be
23:56:40.44
It would be great if you can let me know the regex for that in C#

(^|:)[^=]*=
replaced with
$1
Matches the beginning of a string or a : and everything until and including =.
It is replaced with $1 to keep :.
C#
string strTargetString = #"ABC=23:Qasd=56:Def=40.44";
var myRegex = new Regex(#"(^|:)[^=]*=");
var result = myRegex.Replace(strTargetString, #"$1");
//result: 23:56:40.44
More examples:
ABC=hello:Qasd=56:Def=40.44 => hello:56:40.44

Match
^[^=]+=|(?<=:)[^=]+=
and replace with string.Empty
Regex.Replace("ABC=23:Qasd=56:Def=40.44", #"^[^=]+=|(?<=:)[^=]+=", string.Empty);

Related

grab text between two patern

Check the code bellow. I want to grab everything between this id="a-popover-sp-info-popover- until ". I already tried to use following Regex.Match formula but there is syntax error. Its not valid in c#. How can i do this in proper way. My goal is to grab ABC123 text.
string foo = #id="a-popover-sp-info-popover-ABC123";
string output = Regex.Match(foo, #"id="a-popover-sp-info-popover-(.*)"").Groups[1].Value;
i need to grab only text: ABC123
since your pattern is so rigid, actually the string.Split method could also do the trick:
string output1 = foo.Split(new string[] {"info-popover-"}, StringSplitOptions.RemoveEmptyEntries)
.Last()
.TrimEnd('"');
Console.WriteLine(output1);
Output:
ABC123
You have to make sure to surround your strings with quotation marks ".
If you want to have quotation marks inside of your string you have to escape them with a backslash:
string foo = "id=\"a-popover-sp-info-popover-ABC123\"";
string output = Regex.Match(foo, "id=\"a-popover-sp-info-popover-(.*)\"").Groups[1].Value;
string pattern = "id=\"a-popover-sp-info-popover-[A-Z]{3}[1-9]{3}\"";
string input = "id=\"a-popover-sp-info-popover-ABC123\"";
Match m = Regex.Match(input, pattern);
if (m.Success) Console.WriteLine("Found '{0}'", m.Value);

How to remove multiple first characters using regex?

I have string string A = "... :-ggw..-:p";
using regex: string B = Regex.Replace(A, #"^\.+|:|-|", "").Trim();
My Output isggw..p.
What I want is ggw..-:p.
Thanks
You may use a character class with your symbols and whitespace shorthand character class:
string B = Regex.Replace(A, #"^[.:\s-]+", "");
See the regex demo
Details
^ - start of string
[.:\s-]+ - one or more characters defined in the character class.
Note that there is no need escaping . inside [...]. The - does not have to be escaped since it is at the end of the character class.
A regex isn't necessary if you only want to trim specific characters from the start of a string. System.String.TrimStart() will do the job:
var source = "... :-ggw..-:p";
var charsToTrim = " .:-".ToCharArray();
var result = source.TrimStart(charsToTrim);
Console.WriteLine(result);
// Result is 'ggw..-:p'

c# regex replace everything including a word base64, with nothing and keeping rest of the string

I am wanting to take a string and find base64, and get rid of that and everything prior to that
example
"asdfjljlkjaldf_base64,234u0909230948098234082304802384023094"
Notice "base64," ... I want ONLY everything after "base64,"
Desired: "234u0909230948098234082304802384023094"
I was looking at this code
"string test = "hello, base64, matching";
string regexStrTest;
regexStrTest = #"test\s\w+";
MatchCollection m1 = Regex.Matches(base64,, regexStrTest);
//gets the second matched value
string value = m1[1].Value;
but that is not quite what I want..
Why regular expressions? IndexOf + Substring seems to be quite enough:
string source = "asdfjljlkjaldf_base64,234u0909230948098234082304802384023094";
string tag = "base64,";
string result = source.Substring(source.IndexOf(tag) + tag.Length);
You tried a regex that matches test, a whitespace, and 1+ word chars. The input string just did not match it.
You may use
var results = Regex.Matches(s, #"base64,(\w+)")
.Cast<Match>()
.Select(m => m.Groups[1].Value)
.ToList();
See the regex demo.
The pattern matches base64, substring and then captures into Group 1 one or more word chars with (\w+) pattern. The captured value is stored inside match.Groups[1].Value, just what you get with .Select(m => m.Groups[1].Value).
Some of the other answers are good. Here is a very simple regex
string yourData = "asdfjljlkjaldf_base64,234u0909230948098234082304802384023094";
var newString = Regex.Replace(yourData, "^.*base64,", "");

How to get only letters from a string in C#?

I have a string and need the letters from said string.
string s = "EMA123_33"; // I need "EMA"
string s = "EMADRR123_33"; // I need "EMADRR"
I am using C# in Visual Studio 2008.
You can try this:
var myString = "EMA123_33";
var onlyLetters = new String(myString.Where(Char.IsLetter).ToArray());
please note: this version will find "e" just like "E" - if you need only upper-case letters then do something like this:
var myString = "EMA123_33";
var onlyLetters = new String(myString.Where(c => Char.IsLetter(c) && Char.IsUpper(c)).ToArray());
You can use a regular expression to replace all non-letters:
string s2 = Regex.Replace(s, #"[^A-Z]+", String.Empty);
If you're just after the initial letters, i.e. those at the start of the string (your examples are a bit unclear in that I don't know what would happen to letters at the end of the string), you can use a different Regex:
string s2 = Regex.Replace(s, #"(\p{L}+).*", "$1");
Regex MyRegex = new Regex("[^a-z]", RegexOptions.IgnoreCase);
string s = MyRegex.Replace(#"your 76% strings &*81 gose _ here and collect you want_{ (7 438 ?. !`", #"");
Console.WriteLine(s);
output
yourstringsgosehereandcollecyouwant

Replace any character in between AnyText: and <usernameredacted#example.com> with an empty string using Regex?

I have this string
AnyText: "TomDickHarry" <usernameredacted#example.com>
Desired Output Using Regex
AnyText: <usernameredacted#example.com>
Help to replace anything in between AnyText: and <usernameredacted#example.com> with an empty string using Regex.
I am still a rookie at regular expressions. Could anyone out there help me with the matching & replacing expression for the above scenario?
string ABC = "AnyText: \"TomDickHarry\" <usernameredacted#example.com>"
Regex RemoveName = new Regex("(?<=AnyText:).*(?=<)");
string XYZ = RemoveName.Replace(ABC, "");
So, this will find a regex match in the string you supplied, and on the third line, replace it with an empty string.
const string Input = #"AnyText: ""TomDickHarry"" <usernameredacted#example.com>This is.";
var result = Regex.Replace(Input, "(?<=AnyText:)([^<]*)", string.Empty);
This works for me:
string s = Regex.Replace(Input, ":(.*)<", "");

Categories