0

How do I get application name from path in grok/ regex?

Example path:

C:\Temp\Logs\MyMainApp.SubApp.App.exe.010519.201123.9320.log

Expected Output:

MyMainApp.SubApp.App

Note:

  • App name always ends in .exe
  • Path may have more or less depth in terms of directories

Test:

^(.+?)/([\w]+\.exe)$

with C:\Temp\Logs\MyMainApp.SubApp.App.exe.010519.201123.9320.log

Also tried several options from similar post

Cannon
  • 2,725
  • 10
  • 45
  • 86

2 Answers2

0

For regex, this should work (might need some tweaking for your specific use case / environment).

^.*\\([^\\]+)\.exe\.*
0

You could repeat as least as possible word characters separated by a dot until .exe after the last backslash.

The value is in capture group 1.

^.*\\(\w+(?:\.\w+)*?)\.exe[^\\\r\n]*$

The pattern matches:

  • ^.*\\ Match until the last occurrence of \
  • ( Capture group 1
    • \w+(?:\.\w+)*? match 1+ word chars and repeat as least as possible matching . and 1+ word chars
  • ) close group 1
  • \.exe[^\\\r\n]*$ Match .exe followed by any char except \ until the end of the string

Regex demo

The fourth bird
  • 154,723
  • 16
  • 55
  • 70