Sumit Chourasia Sumit Chourasia - 2 months ago 9
C# Question

How to get dynamic word from particular pattern using Regex in C#

I'm trying to parse my .cs class which is having module aggregator. I want to get the Name of all the concrete class which i'm passing in those module handlers.
so I want to get the Name "ConfigurationContextBuilder" from the following line.

ModuleHandlerAggregator.TryCreateOrGet<ConfigurationContextBuilder>()


I tried following regex code but i'm not getting desired word.

/ModuleHandlerAggregator.TryCreateOrGet<.*?>()/g

Answer

., (, and ) are used in regex patterns, so you need to escape them using a backslash to make them match literally.

So the following regular expression will fix your problem:

ModuleHandlerAggregator\.TryCreateOrGet<(.*?)>\(\)