How can I extract a substring using regular expressions, given only the index?

Is there a way to extract a portion of a string / sentence, given only the from and to the index of the start and end position of the substring? For example: "this is example00001, etc.". and I need to get a substring from position 10-15 (i.e., example) using a regex.

+3


source to share


2 answers


Use a splash screen to commit.

Using your example position 10-15:

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

      




If appearance is not supported, use group 1 of:

^.{10}(.{5})

      

+1


source


I think you need position 11 to get the match you want. Here's an example:

$ cat input.txt
This is an example00001. and so on.
$ sed -r 's|(.{10})(.{5})(.*)|\2|' input.txt
 exam
$ sed -r 's|(.{11})(.{5})(.*)|\2|' input.txt
examp

      

What it is:



    -r      extended regular expressions (only on gnu sed) 
    s       for substitution  
    |       for separator  
    (.{11}) for the first group of any 11 characters (you might want 10)  
    (.{5})  for the second group of any 5 characters 
    (.*)    for any other character, not really needed though  
    \2      for replacing with the second group

      

You can use the ^ and $ characters in your regex for the beginning and end of the string.

+1


source







All Articles