Page 1 of 1

End of line period

Posted: Sat Jul 25, 2009 12:48 am
by George Musick
How do I add a period "." to the end of each line if there isn't already a ".", "?" or "!" there? To be safe, I think I need to remove trailing spaces first.

Thanks,
George

Posted: Sat Jul 25, 2009 5:28 am
by Bob Hansen
Quick and dirty, not pretty at all.....

1. Add a period to the end of every line.
2. Replace ".." with "."
3. Replace "?." with "?"
4. Replace "!." with "!"

========================
Better solution will be RegEx, should look something like this UNTESTED sample. Should add period if no punctuation, and strip trailing spaces also:

Use this as a test file, insert trailing spaces on two lines as noted.
This line has a period at the end.
This line has NO punctuation
This line has one trailing space
This line has a question mark?
This line has two trailing spaces
This line has an exclamation point!
Search for:^(.*)([^_.?!])(_)*$
Replace with:\1\2.
Note: The _ character is for space character

No access to TestPad right now, so cannot do RegEx tests. The RegEx strings above are best guess, but probably need to be tweaked.

Posted: Sat Jul 25, 2009 5:51 am
by MudGuard
Replace

Code: Select all

([^?!.])$
by

Code: Select all

\1.
if empty lines should get a dot as well, use

Code: Select all

(^|[^?!.])$
instead.

if whitespace after ? or ! or . is allowed replace

Code: Select all

([^?!. \t])([ \t]*)$
or

Code: Select all

(^|[^?!. \t])([ \t]*)$
by

Code: Select all

\1.\2

(of course, posix syntax is needed)

Posted: Sat Jul 25, 2009 6:19 am
by Bob Hansen
Aaah! I see that MudGuard has also submitted his solutions.

I just came back to put up an updated RegEx, turns out to be very similar to his, but he was quicker.

Search for: ([^.?!])(_)*$
Replace with: \1.
Note: The imbedded _ is for the space character.

Again, this will insert the period if no existing punctuation, and will also remove trailing spaces before inserting a final period. If there are trailing spaces after an existing punctuation mark, then an additional period will result.