grab text between two patern - c#

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

Related

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

Regex to replace a string between special characters in c#

I have a situation where I have a string in which I have to replace a part that lies between special characters.
I can do the same using substrings and length,but that is the dirty way.
Is there any better way of doing this using regex?
e.g. of the string is
string str1 = "This is the <![CDATA[ SampleDataThatNeedsToBeReplaced ]]";
string repl = "Replacement Text";
I need a regex to get the output as
This is the Replacement Text
I did try a few regex like the following
result = Regex.Replace(str1, #"(?<=CDATA\[)(\w+?)(?=\]\])", repl);
I also tried
Regex x = new Regex("(\\[CDATA\\])(.*?)(\\[\\]\\]\\])");
string Result = str1.Replace(text, "$1" + repl + "$3");
did not get any results.
Any help is appreciated.
Regex.Replace (
"This is the <![CDATA[ SampleDataThatNeedsToBeReplaced ]]",
#"<!\[CDATA\[(.+)]]",
"Replacement Text");
Note that in case you need it ; the old text (between the inner brackets) is available as group1 (and so can be referenced via $1)

Using regex to remove everything that is not in between '<#'something'#>' and replace it with commas

I have a string, for example
<#String1#> + <#String2#> , <#String3#> --<#String4#>
And I want to use regex/string manipulation to get the following result:
<#String1#>,<#String2#>,<#String3#>,<#String4#>
I don't really have any experience doing this, any tips?
There are multiple ways to do something like this, and it depends on exactly what you need. However, if you want to use a single regex operation to do it, and you only want to fix stuff that comes between the bracketed strings, then you could do this:
string input = "<#String1#> + <#String2#> , <#String3#> --<#String4#>";
string pattern = "(?<=>)[^<>]+(?=<)";
string replacement = ",";
string result = Regex.Replace(input, pattern, replacement);
The pattern uses [^<>]+ to match any non-pointy-bracket characters, but it combines it with a look-behind statement ((?<=>)) and a look-ahead statement (?=<) to make sure that it only matches text that occurs between a closing and another opening set of brackets.
If you need to remove text that comes before the first < or after the last >, or if you find the look-around statements confusing, you may want to consider simply matching the text that comes between the brackets and then loop through all the matches and build a new string yourself, rather than using the RegEx.Replace method. For instance:
string input = "sdfg<#String1#> + <#String2#> , <#String3#> --<#String4#>ag";
string pattern = #"<[^<>]+>";
List<String> values = new List<string>();
foreach (Match m in Regex.Matches(input, pattern))
values.Add(m.Value);
string result = String.Join(",", values);
Or, the same thing using LINQ:
string input = "sdfg<#String1#> + <#String2#> , <#String3#> --<#String4#>ag";
string pattern = #"<[^<>]+>";
string result = String.Join(",", Regex.Matches(input, pattern).Cast<Match>().Select(x => x.Value));
If you're just after string manipulation and don't necessarily need a regex, you could simply use the string.Replace method.
yourString = yourString.Replace("#> + <#", "#>,<#");

Regex to match pattern in 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);

Categories