1

I am looking to extract particular string from path.

For example, I have to extract 4th value separated by (.) from filename. which is "lm" in below examples.

Examples:

/apps/java/logs/abc.defgh.ijk.lm.nopqrst.uvw.xyz.log
/apps2/java/logs/abc.defgh.ijk.lm.log

This will extract full file name:

.*\/(?<name>.*).log
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Ankit Goyal
  • 151
  • 1
  • 12

2 Answers2

1

You can use

.*\/(?:[^.\/]*\.){3}(?<value>[^.\/]*)[^\/]*$

Or, if .log must be the extension:

.*\/(?:[^.\/]*\.){3}(?<value>[^.\/]*)[^\/]*\.log$

See the regex demo. Details:

  • .* - any zero or more chars other than line break chars, as many as possible
  • \/ - a / char
  • (?:[^.\/]*\.){3} - three occurrences of zero or more chars other than . and / as many as possible and a dot
  • (?<value>[^.\/]*) - Group "value": zero or more chars other than . and / as many as possible
  • [^\/]* - zero or more chars other than /
  • \.log - a .log substring
  • $ - end of string.
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
0

You can also try

\/(?:\w+\.){3}(\w+)

Or

\/(?:\w+\.){3}(\w+).*\.log

Where:

  • \/ - Match string starting from "/"
  • (?:\w+\.){3} - Matches 3 occurrences of "xyz." e.g. abc.defgh.ijk.
  • (\w+) - Capture the alpanumeric string. This will contain the target value e.g. "lm"
  • .*\.log - Optional. Match any set of characters that ends with .log e.g. .nopqrst.uvw.xyz.log