❔ Help with simple regex
How do I use regex replace to transform a string with four numbers to have dots between them? Like this:
1234 -> 1.2.3.4
3690 -> 3.6.9.0
1234 -> 1.2.3.4
3690 -> 3.6.9.0
Compile: 558.679ms | Execution: 77.172ms | React with ❌ to remove this embed.
/close (\d)$1\.(\d)(\d)(\d)(\d)$1\.$2\.$3\.$4\.Regex.Replace1234: 1.2.3.4
3453454: 3.4.5.3.4.5.4
343: 3.4.3
343455: 3.4.3.4.5.5@""| Method | input | Mean | Error | StdDev | Gen0 | Allocated |
|----------------------------------------- |-------------------- |------------:|----------:|----------:|-------:|----------:|
| DottedNumbers_StringJoin | 123 | 49.77 ns | 1.014 ns | 0.996 ns | 0.0081 | 136 B |
| DottedNumbers_StringJoin | 1337 | 62.06 ns | 1.207 ns | 1.129 ns | 0.0100 | 168 B |
| DottedNumbers_StringJoin | 0123456789 | 121.12 ns | 2.404 ns | 3.209 ns | 0.0200 | 336 B |
| DottedNumbers_StringJoin | 0123(...)6789 [100] | 986.00 ns | 19.298 ns | 27.677 ns | 0.1707 | 2856 B |
| DottedNumbers_Regex_LookBehindLookAhead | 123 | 162.02 ns | 2.115 ns | 1.875 ns | 0.0019 | 32 B |
| DottedNumbers_Regex_LookBehindLookAhead | 1337 | 206.13 ns | 0.906 ns | 0.757 ns | 0.0024 | 40 B |
| DottedNumbers_Regex_LookBehindLookAhead | 0123456789 | 466.24 ns | 2.322 ns | 2.058 ns | 0.0038 | 64 B |
| DottedNumbers_Regex_LookBehindLookAhead | 0123(...)6789 [100] | 4,050.07 ns | 50.166 ns | 46.925 ns | 0.0229 | 424 B |
| DottedNumbers_Regex_SingleDigitLookAhead | 123 | 217.83 ns | 0.724 ns | 0.677 ns | 0.0019 | 32 B |
| DottedNumbers_Regex_SingleDigitLookAhead | 1337 | 293.45 ns | 1.291 ns | 1.144 ns | 0.0024 | 40 B |
| DottedNumbers_Regex_SingleDigitLookAhead | 0123456789 | 684.73 ns | 12.974 ns | 12.742 ns | 0.0038 | 64 B |
| DottedNumbers_Regex_SingleDigitLookAhead | 0123(...)6789 [100] | 6,220.14 ns | 67.293 ns | 59.653 ns | 0.0229 | 424 B |values:string.Join('.', "1234".ToCharArray())1.2.3.41.2.3.4Regex.Replace("1234", @"(\d)", "$1.")1.2.3.4.void F(string v) {
Console.WriteLine($"{v}: {Regex.Replace(v, """(?<!^)(?=\d)""", ".")}");
}
F("1234");
F("3453454");
F("343");
F("343455");Regex.Replace("1234", @"(\d)(?=\d)", "$1.")[MemoryDiagnoser]
[Orderer(SummaryOrderPolicy.FastestToSlowest, MethodOrderPolicy.Alphabetical)]
public class Benchmarks
{
[Benchmark]
[Arguments("123")]
[Arguments("1337")]
[Arguments("0123456789")]
[Arguments("0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789")]
public string DottedNumbers_StringJoin(string input)
{
return string.Join('.', values: input);
}
[Benchmark]
[Arguments("123")]
[Arguments("1337")]
[Arguments("0123456789")]
[Arguments("0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789")]
public string DottedNumbers_Regex_LookBehindLookAhead(string input)
{
return Regexes.GetLookBehindLookAheadRegex().Replace(input, ".");
}
[Benchmark]
[Arguments("123")]
[Arguments("1337")]
[Arguments("0123456789")]
[Arguments("0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789")]
public string DottedNumbers_Regex_SingleDigitLookAhead(string input)
{
return Regexes.GetSingleDigitLookAheadRegex().Replace(input, "$1.");
}
}
public static partial class Regexes
{
[GeneratedRegex(@"(?<!^)(?=\d)", RegexOptions.Compiled)]
public static partial Regex GetLookBehindLookAheadRegex();
[GeneratedRegex(@"(\d)(?=\d)", RegexOptions.Compiled)]
public static partial Regex GetSingleDigitLookAheadRegex();
}