Home > Software design >  using Regex how do I capture the exception message only without stack trace
using Regex how do I capture the exception message only without stack trace

Time:08-24

I have this text:

System.FormatException: String '' was not recognized as a valid DateTime. at Application.Mappers.AutoMapperProfile.<>c.<.ctor>b__0_92(CartCampaignDto x, CartCampaign y) in /build/src/Application/Mappers/AutoMapperProfile.cs:line 60 at lambda_method543(Closure , Object , Cart , ResolutionContext ) --- End of inner exception stack trace --- at lambda_method543(Closure , Object , Cart , ResolutionContext ) --- End of inner exception stack trace --- 

I'm trying to match only this part

System.FormatException: String '' was not recognized as a valid DateTime.

I tried

^(.*) at.*)

^(. )(?=at ){1}(.*)

but it didn't work.

EDIT:

This is not in any programming language, this is to build a NewRelic dashboard where you can add a Regex to get some insights.

EDIT 2 NewRelic uses Google/R2 Regex

EDIT 3 I'm trying to build this query

SELECT uniqueCount(1) From Log where `@l` = 'Error' FACET capture(`@x`, r'(?P<anyvarname>^(.*?\.)\s*at\b)')

EDIT 4 Not sure why this doesn't work

enter image description here

CodePudding user response:

I think this will work for your:

^. \.\s

Quickly breaking it in pieces:

  • ^. -> matches, from the beggining of the line, any 1 or more characters
  • \.\s -> matches a dot followed by a whitespace.

testing the regex

CodePudding user response:

You could write the pattern matching the whole line with a named capture group:

(?P<anyvarname>(.*?\.)\s*at\b).*

See these pages for more information about capture

  • Related