2

I am looking for a regex that matches any characters 11 times after any characters present 56 times.

example:

this is a long formatted line with any char. 56    timesmatchI wantNothingShouldMatchHere
  • What I want:

    matchI want
    
  • What I tried:

    (?<=.{56}).{11}
    

    but this matches matchI want AND NothingShou AND ldMatchHere.

  • This:

    (?<=.{56}).{11}?
    

    yields the same.

Thank you for you help !


Note: This regex is aimed to be used with the re python module in my case

3
  • 2
    If you would explain how you want to use that RegEx (which tool do you want to use, and to what end you want to match that string), contributors might point you to other ways to accomplish the task. – AdminBee Oct 8 '20 at 11:20
  • Good recommendation @AdminBee, I've just added a note – jeremy Oct 8 '20 at 11:49
  • See also ^.{56}\K.{11} with perl-like regexps ((?<=...) is also a perl regexp operator) – Stéphane Chazelas Oct 8 '20 at 12:03
1

This should work; the ^ forces the 56 characters to be at the beginning of the line:

(?<=^.{56}).{11}

Demo

0
1
sed -E 's/.{56}(.{11}).*/\1/'

The idea with Sed is to match the whole line and only capture the desired 11 characters. They are captured inside the () and the whole line is replaced by that group. See Using \1 to keep part of the pattern for a more detailed explanation.

If you want to ignore lines that do not contain at least 56+11=67 characters, use

sed -nE 's/.{56}(.{11}).*/\1/p' file
1
  • 1
    I was looking only for the regex in order to use it with the re python module but this is still good to see how to make it with sed ! Thanks – jeremy Oct 8 '20 at 11:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.