Search code examples
regexlogstash-grok

How to extract substring using regex given only the index?


Is there any way to extract part of string/sentence, given only the from and to index of starting and ending position of the substring? Eg: "this is an example00001. and so on." and I need to get substring from position 10 to 15 (ie., examp) using regex.


Solution

  • Use a look behind anchored to start.

    Using your example of position 10 to 15:

    (?<=^.{10}).{5}
    

    If look behind is not supported, use group 1 of:

    ^.{10}(.{5})