Replace string

Peter Volz 1,295 Reputation points

Hello everyone,

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

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: port 25


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

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
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

    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