Replace string

Peter Volz 1,295 Reputation points
2023-04-01T18:24:34.16+00:00

Hello everyone,

I have a string that contains some IP and port numbers in this format:

108.177.3.26:25

each block in IP can be from 1 to 3 digits, and port after : can be up to 5 digits (65535)

No need to validate it, just gonna replace all those blocks with something like:

108.177.3.26 port 25

Now:

data string = data string . Replace (":", " port ")

seems the easiest, but will also replace all other : in the source data, any short way to do it?

or need to parse chars before and after :

Thanks for help

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,348 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,594 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 112.8K Reputation points
    2023-04-01T18:36:17.6733333+00:00

    Check this (in C#):

    data_string = Regex.Replace( data_string, @"(?<=(?<!\d)(\d{1,3}\.){3}\d{1,3}):(?=\d{1,5}(?!\d))", " port " );
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful