C# regular expression (replace) - c#

Assume I have the string:
10,11,12,13,14,ABC,DEF,GHI,66
I am looking to run a regex against it to only return 0-9 and the "," characters, essentially stripping anything else out.
I have looked at Regex.Replace, but something isn't quite right with it. My code below:
Regex reg = new Regex(#"[0-9,]+");
string input = reg.Replace(input, delegate(Match m)
{
return String.Empty;
});
How can I make this work?

Do you just want a ^ in that?
input = Regex.Replace(input, #"[^0-9,]+", "");

Would a match collection give you more control?
Using \d+[^,] you can get a collection of digits?
You could then loop through your collection and recreate your desired string.
using linq you could do the following:
var input = "10,11,12,13,14,ABC,DEF,GHI,66";
Regex re = new Regex(#"\d+[^,]");
input = (from Match m in re.Matches(input) select m.Value).Aggregate("", (acc, item) => acc + "," + item).TrimStart(',');

How about this:
var testString = "10,11,12,13,14,ABC,DEF,GHI,66";
var split = testString.Split(',');
var result = String.Join(",", split.Where(element => element.All(c => Char.IsDigit(c))).ToArray());

I think that you may do it without regular expressions via analysis of what is required for your characters in set [0123456789,].

Related

How do I split on spaces, keeping spaces in quotes and not lose my equals

I am no good with RegEx so not real familiar with what is going on in this code I borrowed. I want to split the following String:
CHARM CARD_SLOT=1 IO_SUBSYSTEM="CHARMS" CONTROLLER="CIOC-CB3-IO" DEFINITION="CHMIO_DO_24_VDC_HIGH-SIDE_CHARM"
So that is in individual strings like: (What I want to return)
CHARM CARD_SLOT=1
IO_SUBSYSTEM="CHARMS"
CONTROLLER="CIOC-CB3-IO"
DEFINITION="CHMIO_DO_24_VDC_HIGH-SIDE_CHARM"
I am using this code:
while (null != (workString = s.ReadLine()))
{
RegexOptions options = RegexOptions.None;
Regex regex = new Regex(#"((""((?<token>.*?)(?<!\\)"")|(?<token>[\w]+))(\s)*)", options);
var result = (from Match m in regex.Matches(workString)
where m.Groups["token"].Success
select m.Groups["token"].Value).ToList();
foreach (string o in result)
{
if (!o.Contains("{") || !o.Contains("}"))
{
endResult = endResult + "\r\n" + o;
Console.WriteLine("'{0}'", "\r\n" + o);
}
}
}
What I am currently returning
CHARM
CARD_SLOT
1
IO_SUBSYSTEM
CHARMS
CONTROLLER
CIOC-CB3-IO
DEFINITION
CHMIO_DO_24_VDC_HIGH-SIDE_CHARM
This matches your example:
var pattern = #"(\w[\w ]+=(?:""[^""]+""|[^ ]+))+";
var optionsList = Regex.Matches(src, pattern).Cast<Match>().Select(m => m.Value).ToList();
this pattern should work:
pattern = #"[^=]+=[^=]+(\s|.$)"; //matches with spaces
pattern = #"[^=\s]+=[^=]+(?=(\s|.$))"; //matches without spaces
my idea is to take = and "look around" on both sides (especially right side).
In both pattern I look whether it ends with " " or it is very end of string. And finally I remove matched spaces by using lookahead feature as we don't need them.

Matching a result in c#

I have the following string
background-image: url('https://s3-eu-west-1.amazonaws.com/files.domain.com/uploads/image/file/168726/carousel_IMG_6455.jpg')
and I just want to get the image URL.
My code is:
image = image.Replace(#"'", "\"");
Match match = Regex.Match(image, #"'([^']*)");
Match.Success returns nothing, so I can not get the image URL.
Is there something missing? This used to work but not now.
The following pattern achieves your result, without the usage of string.replace.
var pattern = #"'(?<url>.*)'";
Match match = Regex.Match(image, pattern);
Console.WriteLine($"Math: {match.Groups["url"].Value}");
If you want the " surrounding the string, add this:
var result = $"\"{match.Groups["url"].Value}\""
No need for a regex, just
Split the string with ' substring
Find the element starting with http
Return the first found item.
C# demo:
var s = "background-image: url('https://s3-eu-west-1.amazonaws.com/files.domain.com/uploads/image/file/168726/carousel_IMG_6455.jpg')";
var res = s.Split(new[] {"'"}, StringSplitOptions.None)
.Where(v => v.StartsWith("http"))
.FirstOrDefault();
Console.WriteLine(res);
// => https://s3-eu-west-1.amazonaws.com/files.domain.com/uploads/image/file/168726/carousel_IMG_6455.jpg
If you need to use a regex, use the standard regex to match a string between two strings, start(.*?)end where (.*?) captures into Group 1 any 0 or more chars other than a newline, as few as possible as the *? quantifier is lazy:
var s = "background-image: url('https://s3-eu-west-1.amazonaws.com/files.domain.com/uploads/image/file/168726/carousel_IMG_6455.jpg')";
var res = Regex.Match(s, #"'(.*?)'").Groups[1].Value ?? string.Empty;
Console.WriteLine(res);
// => https://s3-eu-west-1.amazonaws.com/files.domain.com/uploads/image/file/168726/carousel_IMG_6455.jpg
See another C# demo
The regex of: (\".*\")
Will match the URL given the input string:
background-image: url("https://s3-eu-west-1.amazonaws.com/files.domain.com/uploads/image/file/168726/carousel_IMG_6455.jpg")
image = image.Replace(#"'", "\"");
Match match = Regex.Match(image, "(\\\".*\\\")");
Edit:
If you are looking for something that will match pairs of single quotes or double quotes you could use:
(\".*\"|'.*')
Match match = Regex.Match(image, "(\\\".*\\\"|'.*')");

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

start from end of string till special character c#

This is probably a super easy question but I can't seem to figure it out. Do you know how to extract just the portion after the '/' in a string. So for like the following:
[HKEY_LOCAL_MACHINE\SOFTWARE\4YourSoul\Server\ReportEMailService\OrderConfirmation_SynergyWorldInc]
So I just want the 'OrderConfirmation_SynergyWorldInc' portion. I got 271 entries where I gotta extract just the end portion (the all have the portions before that in all of them, if that helps).
Thanks!!
IF YOU HAVE A SINGLE ENTRY...
You need to use LastIndexOf with Substring after a bit of trimming:
var s = #"[HKEY_LOCAL_MACHINE\SOFTWARE\4YourSoul\Server\ReportEMailService\OrderConfirmation_SynergyWorldInc]";
s = s.Trim('[',']');
Console.Write(s.Substring(s.LastIndexOf('\\') + 1));
Result: OrderConfirmation_SynergyWorldInc
IF YOU HAVE MULTIPLE RECORDS...
You can use a regex to extract multiple matches from a large text containing [...] substring:
[^\\\[\]]+(?=\])
See demo
For [HKEY_LOCAL_MACHINE\SOFTWARE\4YourSoul\Server\ReportEMailService\OrderConfirmation_SynergyWorldInc][SOMEENTRY] string, you will then get 2 results:
The regex matches
[^\\\[\]]+ - 1 or more characters other than ], [ and \
(?=\]) - before the end ] character.
C#:
var results = Regex.Matches(s, #"[^\\\[\]]+(?=\])").OfType<Match>().Select(p => p.Value).ToList();
var s = #"[HKEY_LOCAL_MACHINE\SOFTWARE\4YourSoul\Server\ReportEMailService\OrderConfirmation_SynergyWorldInc]";
Console.WriteLine (s.Trim(']').Split('\\').Last());
prints
OrderConfirmation_SynergyWorldInc
var key = #"[HKEY_LOCAL_MACHINE\SOFTWARE\4YourSoul\Server\ReportEMailService\OrderConfirmation_SynergyWorldInc]";
key = key.Replace("[", string.Empty);
key = key.Replace("]", string.Empty);
var splitkeys =key.Split('\\');
if (splitkeys.Any())
{
string result = splitkeys.Last();
}
Try:
string orderConfirmation = yourString.Split(new []{'\\\'}).Last();
You may also want to remove the last character if the brackets are included in the string.
string pattern = ".*\\(\w*)";
string value = "[HKEY_LOCAL_MACHINE\SOFTWARE\4YourSoul\Server\ReportEMailService\OrderConfirmation_SynergyWorldInc]"
Regex r = new Regex(pattern);
Match m = r.Match(value);

Regular expression to split string and number

I have a string of the form:
codename123
Is there a regular expression that can be used with Regex.Split() to split the alphabetic part and the numeric part into a two-element string array?
I know you asked for the Split method, but as an alternative you could use named capturing groups:
var numAlpha = new Regex("(?<Alpha>[a-zA-Z]*)(?<Numeric>[0-9]*)");
var match = numAlpha.Match("codename123");
var alpha = match.Groups["Alpha"].Value;
var num = match.Groups["Numeric"].Value;
splitArray = Regex.Split("codename123", #"(?<=\p{L})(?=\p{N})");
will split between a Unicode letter and a Unicode digit.
Regex is a little heavy handed for this, if your string is always of that form. You could use
"codename123".IndexOfAny(new char[] {'1','2','3','4','5','6','7','8','9','0'})
and two calls to Substring.
A little verbose, but
Regex.Split( "codename123", #"(?<=[a-zA-Z])(?=\d)" );
Can you be more specific about your requirements? Maybe a few other input examples.
IMO, it would be a lot easier to find matches, like:
Regex.Matches("codename123", #"[a-zA-Z]+|\d+")
.Cast<Match>()
.Select(m => m.Value)
.ToArray();
rather than to use Regex.Split.
Well, is a one-line only: Regex.Split("codename123", "^([a-z]+)");
Another simpler way is
string originalstring = "codename123";
string alphabets = string.empty;
string numbers = string.empty;
foreach (char item in mainstring)
{
if (Char.IsLetter(item))
alphabets += item;
if (Char.IsNumber(item))
numbers += item;
}
this code is written in java/logic should be same elsewhere
public String splitStringAndNumber(String string) {
String pattern = "(?<Alpha>[a-zA-Z]*)(?<Numeric>[0-9]*)";
Pattern p = Pattern.compile(pattern);
Matcher m = p.matcher(string);
if (m.find()) {
return (m.group(1) + " " + m.group(2));
}
return "";
}

Categories