❔ 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
18 Replies
ero
ero2y ago
why would you use regex for that?
MODiX
MODiX2y ago
Ero#1111
REPL Result: Success
string.Join('.', "1234".ToCharArray())
string.Join('.', "1234".ToCharArray())
Result: string
1.2.3.4
1.2.3.4
Compile: 437.637ms | Execution: 28.294ms | React with ❌ to remove this embed.
Cliff Karlsson
Thanks that is probably easier, nut just of curiosity how would I do the same using regex ?
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
D.Mentia
D.Mentia2y ago
and if you're looking to make an IP address, you can google specific regexes that can do that (they get kinda complicated) You could do it either with regex (\d) and replace with $1\. and do a search/replace for all occurences on the string. Or if you want to ensure 4 numbers, (\d)(\d)(\d)(\d) and $1\.$2\.$3\.$4\.. But either way regex isn't really very good at that and the string joins are way easier and cleaner And I can't remember how C# identifies the groups in replace, it might be \1 if it's not $1
ero
ero2y ago
there might be a way to use Regex.Replace
MODiX
MODiX2y ago
Ero#1111
REPL Result: Success
Regex.Replace("1234", @"(\d)", "$1.")
Regex.Replace("1234", @"(\d)", "$1.")
Result: string
1.2.3.4.
1.2.3.4.
Compile: 449.694ms | Execution: 28.315ms | React with ❌ to remove this embed.
ero
ero2y ago
but i don't know how to exclude that final period you'd have to match a single digit, but only when it's followed by another digit
MODiX
MODiX2y ago
Retax#0813
REPL Result: Success
void F(string v) {
Console.WriteLine($"{v}: {Regex.Replace(v, """(?<!^)(?=\d)""", ".")}");
}

F("1234");
F("3453454");
F("343");
F("343455");
void F(string v) {
Console.WriteLine($"{v}: {Regex.Replace(v, """(?<!^)(?=\d)""", ".")}");
}

F("1234");
F("3453454");
F("343");
F("343455");
Console Output
1234: 1.2.3.4
3453454: 3.4.5.3.4.5.4
343: 3.4.3
343455: 3.4.3.4.5.5
1234: 1.2.3.4
3453454: 3.4.5.3.4.5.4
343: 3.4.3
343455: 3.4.3.4.5.5
Compile: 558.679ms | Execution: 77.172ms | React with ❌ to remove this embed.
ero
ero2y ago
unnecessary use of raw string imo yeah but @"" most insane thing to say 1 right? yeah so this works too
MODiX
MODiX2y ago
Ero#1111
REPL Result: Success
Regex.Replace("1234", @"(\d)(?=\d)", "$1.")
Regex.Replace("1234", @"(\d)(?=\d)", "$1.")
Result: string
1.2.3.4
1.2.3.4
Compile: 368.483ms | Execution: 23.244ms | React with ❌ to remove this embed.
ero
ero2y ago
forgor about positive lookahead
| 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 |
| 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 |
[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();
}
[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();
}
surprised my version is slower, honestly i knew you could, but couldn't be bothered figuring it out just coding in discord lol the allocated memory is crazy with string.Join though maybe you can be faster with string.Create let me try
amio
amio2y ago
ToCharArray is redundant (missed the new code, nvm)
ero
ero2y ago
not if you don't specify values:
amio
amio2y ago
oh, you're right. That's... unintuitive
ero
ero2y ago
https://paste.mod.gg/msmstqypndke/2 no surprises there span triumphs over all, once again
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.