question

BKHariprasad-2463 avatar image
0 Votes"
BKHariprasad-2463 asked BKHariprasad-2463 commented

How to fetch data from string via regex?

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

dotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

Viorel-1 avatar image
0 Votes"
Viorel-1 answered BKHariprasad-2463 commented

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.

· 5
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hello Viorel,
Thanks for answer.
But regex is not working as there is a typo in my string. Refer attached image.
You can find some 19 spaces between receivedfrom and UDP?


Thanks and Regards,
Hari200883-trap.png


0 Votes 0 ·
trap.png (46.2 KiB)
Viorel-1 avatar image Viorel-1 BKHariprasad-2463 ·

To support such strings too, try this pattern: @"^\s*receivedfrom\s+UDP:\s*\[(.+?)\]".



0 Votes 0 ·

Hello Viorel,
Thanks. It is working.

I have one more requirement:
201053-trap.png



From the above screenshot, I need to fetch the highlighted part and maintain in Dictionary key and value.
Like dictionary.Add("ifIndex.19", "19").

Please help on this via regex.

Thanks and Regards,
Hari


0 Votes 0 ·
trap.png (20.4 KiB)
Show more comments

Hello Viorel,
Thanks. It is working my initial request.

Best Regards,
Hari

0 Votes 0 ·