❔ 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
why would you use regex for that?
Ero#1111
REPL Result: Success
Result: string
Compile: 437.637ms | Execution: 28.294ms | React with ❌ to remove this embed.
Thanks that is probably easier, nut just of curiosity how would I do the same using regex ?
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
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 $1there might be a way to use
Regex.Replace
Ero#1111
REPL Result: Success
Result: string
Compile: 449.694ms | Execution: 28.315ms | React with ❌ to remove this embed.
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
Retax#0813
REPL Result: Success
Console Output
Compile: 558.679ms | Execution: 77.172ms | React with ❌ to remove this embed.
unnecessary use of raw string imo
yeah but
@""
most insane thing to say
1 right?
yeah so this works tooEro#1111
REPL Result: Success
Result: string
Compile: 368.483ms | Execution: 23.244ms | React with ❌ to remove this embed.
forgor about positive lookahead
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
ToCharArray is redundant (missed the new code, nvm)
not if you don't specify
values:
oh, you're right. That's... unintuitive
https://paste.mod.gg/msmstqypndke/2
no surprises there
span triumphs over all, once again
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
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.