How to fetch data from string via regex?

B-K Hariprasad 1 Reputation point
2022-05-10T13:05:39.88+00:00

Hell Team,
I have string s= "receivedfrom UDP: [10.36.15.60]:1024->[10.108.247.97]:162";
From this, I need to fetch 10.36.15.60 via regex.
Please help on this.

Thanks and Regards,
Hari

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,234 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Viorel 112.1K Reputation points
    2022-05-10T13:22:13.227+00:00

    Try a simple approach too:

    string s = "receivedfrom UDP: [10.36.15.60]:1024->[10.108.247.97]:162";
    var m = Regex.Match( s, @"^receivedfrom UDP: \[(.+?)\]" );
    string result = m.Success ? m.Groups[1].Value : null;
    

    It can be improved using a more complex expression depending on problem details.