Search code examples
regexnotepad++limit

Notepad++: Delete everything after a number of characters in string


The following is an example of 24 characters per line in Notepadd ++. I need to limit the characters per line to 14 characters.

Hell, how is she today ?

I need it to look like the following:

Hell, how is

I used this code

Find what: ^(.{1,14}).*
Replace with: $1

However, it show "Hell, how is s", it is misspelled.

How can I can limit the number of characters to 14 characters per line in Notepad++ and delete last word ?


Solution

  • This should work for you:

    Find what: ^(.{1,14}(?<=\S)\b).*$

    Replace with: $1

    so for Hell, how is she today ? the output is: Hell, how is

    DEMO

    ^                # The beginning of the string
    (                # Group and capture to \1:
      .{1,14}        # Any character except \n (between 1 and 14 times (matching the most amount possible))
      (?<=\S)        # This lookbehind makes sure the last char is not a space
      \b             # The boundary between a word char (\w). It matches the end of a word in this case
    )                # End of \1
    .*$              # Match any char till the end of the line