3

I want to select all lines of maximum 12 words that end that do not have any punctuation at the end.

Example:

Love's Equal On Earth

In a moving "in" and "out of focus" image, what you see is what you want to believe about yourself.

I love the feeling.

I must confess every day for better feeling

REsTsora is a non-profit website of millions of free books, movies, software, music, websites, and more.

The Output must find:

Love's Equal On Earth
I must confess every day for better feeling

My regex is not too good:

^(.*?)(\w+\s?){1,12}(?!\S|$)

I want to avoid all the punctuation marks at the end of the lines, that is to find those lines of a maximum of 12 words that have nothing after the last word, no punctuation marks, nothing. My regex stops of the line, before the ' punctuation mark.

4
  • I can write a Python code, also, but I need a regex for notepad++ Commented Sep 20, 2023 at 9:04
  • Have you tried regex101? Or regexbuddy?
    – Gantendo
    Commented Sep 20, 2023 at 9:05
  • 1
    I also thought of a solution that I posted, but it doesn't work. Do you think I was writing here without first testing several options? Commented Sep 20, 2023 at 9:10
  • Is Love's counted as one or two words?
    – Zibelas
    Commented Sep 20, 2023 at 18:58

4 Answers 4

3
  • Ctrl+H
  • Find what: ^(?:(?:\w+\W){13}.+|.*[.!?]|)\R
  • Replace with: LEAVE EMPTY
  • TICK Wrap around
  • SELECT Regular expression
  • UNTICK . matches newline
  • Replace all

Explanation:

^           # beginning of line
    (?:         # non capture group
        (?:         # non capture group
            \w+         # 1 or more word characters
            \W          # non word character
        ){13}       # end group, must appear 13 times
        .+          # 1 or more any character
      |           # OR
        .*          # 0 or more any character
        [.!?]       # a punctuation, you can add all punctuation you want
      |           # OR
                    # EMPTY
    )           # end group
    \R          # any kind of linebreak

Screenshot (before):

enter image description here

Screenshot (after):

enter image description here

2
  • the problem on your regex is that it finds also blank lines... Commented Sep 21, 2023 at 16:12
  • @HellenaCrainicu: Yes, it does. I thought you wanted such behaviour regarding your example. If you want to keep blank lines, just remove the last alternative in the regex use: ^(?:(?:\w+\W){13}.+|.*[.!?])\R
    – Toto
    Commented Sep 21, 2023 at 16:32
1
^(\S+ ?){1,12}(?<!\.)$

Flags: gm

  • ^: start of line (\S+ ?) => \S+ any character that isn't a whitespace, repeated 1 to n times
  • followed by a optional whitespace ?
  • All repeated 1 to 12 times {1,12}
  • (?<!\.)$ : $ end of line not preceded by a . (feel free to put more like ?,!;)

Try it here.

What's interesting in this one is that you can have punctuation between your words, it will check only the last punctuation at line's end.

3
  • The OP wrote "...do not have any punctuation at the end". So, I didn't consider punctuation between words :) Commented Sep 20, 2023 at 10:59
  • You're right, I think I misunderstood the intent, I edited the message
    – ColdK
    Commented Sep 20, 2023 at 11:02
  • This is the only correct solution of selecting lines here, but: a) not sure why mention other sentence ending punctuation later instead of including it within regex, b) please don't add [edited] type of parts, we can see something was edited.
    – Destroy666
    Commented Sep 21, 2023 at 11:52
0
  • Ctrl+H
  • Find what: ^(.*?)(\w+\s){1,12}(\w+)\s*$
  • Replace with: (Leave EMpty)
  • CHECK Wrap around
  • CHECK Regular expression
  • UNCHECK . matches newline
  • Replace all
-1

Not sure if this is optimal, but try the following:

  • Find what: ^(\w+[^\.,\:!\?]){1,12}$
  • Search mode: Regular expression

Note: I have only considered a few punctuation characters (. , , , : , ! and ?).

1
  • 1
    This regex wouldn't match the sentence "Hello, world" which fits the description given in the question. Commented Sep 20, 2023 at 19:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .