Page 1 of 1

Can TP b taught 2 find text between parenthesis & remove

Posted: Sun Aug 27, 2006 12:14 am
by rlporter5
I need to edit a lot of documents that have text contained between parenthesis, e.g. (this text is a reference). I want TP to find the text and delete it....the text will be of different lengths...so it will need to find the left paren, count the characters until the right is found, and then delete that number of characters...

Can TP do this? Can any text editor do this?

Posted: Sun Aug 27, 2006 3:44 am
by Bob Hansen
The feature you are looking for is Regular Expressions (RegEx).
TextPad supports RegEx, and can do it like this:

1. From Main Menu, select Configure/Preferences/Editor.
2. Be sure to have a check mark in box to Use POSIX for Regular Expressions. (Not necessary, but provides easier to read expressions).
3. Click Apply/OK to save.
4. In the file you want to edit,from the Main Menu, select Search/Replace.
Search for: \(.*\)
Replace with: \(\)
5. Be sure to have a checkmark in the box for Regular expression.
6. Click on button to Replace All.
7. DONE

If you make a mistake, you can click in the document, and press CTL-Z to undo.

You can check the Help Topics for "regular expression" sections.
And there is plenty of help available here.

Posted: Sun Aug 27, 2006 10:09 am
by SteveH
This will find the text within the brackets and delete it as per the original query. If you would rather have the brackets removed just leave the replacement expression blank.

Be aware though that TextPad will find the longest match possible so if a line contains two sets of parentheses the text from the first opening bracket to the last closing bracket will be removed. The danger is you remove more text that you had planned.

The way round this is to modify the search expression to make it less greedy. For example in an editor such as BBEdit it would become:

Code: Select all

\(.*?\)
Note to self: Must print out TP Regex help file for use on Mac

Posted: Sun Aug 27, 2006 10:58 am
by ben_josephs
You're right about the greediness. Unfortunately, TextPad doesn't support non-greedy repeats (although WildEdit (http://www.textpad.com/products/wildedit/) does). But you can achieve the same effect by not allowing any close parenthesis symbols (or not allowing any open parenthesis symbols) within the parenthesised text, for example,
Find what: \([^)]*\)
Replace with: [nothing]

[X] Regular expression
\( matches a literal open parenthesis symbol
[^)] matches any single character that isn't a close parenthesis symbol
[^)]* matches any number (possibly zero) of characters that aren't close parenthesis symbols
\) matches a literal close parenthesis symbol

Note that parenthesis symbols are not quoted (with a backslash) in character classes ([...]). (Also, they do not need to be quoted in replacement expressions (which are not regular expressions), although it doesn't do any harm.) (The rules for quoting are different in WildEdit.)

This assumes you are using Posix regular expression syntax:
Configure | Preferences | Editor

[X] Use POSIX regular expression syntax

Posted: Sun Aug 27, 2006 4:40 pm
by Bob Hansen
Nice touch ben_josephs. I did not consider multiple instances on the line.

Posted: Sun Aug 27, 2006 4:58 pm
by SteveH
you can achieve the same effect by not allowing any close parenthesis symbols (or not allowing any open parenthesis symbols) within the parenthesised text
I had seen that trick used elsewhere in the forum but never really appreciated the logic behind it till this explanation. Definitely a nice workaround till TP gets a better regex engine.

Thanks!

Posted: Sun Aug 27, 2006 5:16 pm
by rlporter5
Wow - elegant solutions and what a fast community response - I am amazed and very grateful...I will give this a try now. Thank you all very much!!

I posted another question for this community as well - thanks again!

Bob

Posted: Tue Aug 29, 2006 3:22 pm
by Ed
If the text within the parentheses crosses end of lines, then first change newline characters to (say) ¬ remove parenthetical text and then convert ¬ to newlines.

To change newlines to ¬
Search for \n
Change to ¬

With regular expression checked.