Can TP b taught 2 find text between parenthesis & remove

General questions about using TextPad

Moderators: AmigoJack, helios, bbadmin, Bob Hansen, MudGuard

Post Reply
rlporter5
Posts: 4
Joined: Sun Aug 27, 2006 12:08 am

Can TP b taught 2 find text between parenthesis & remove

Post 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?
User avatar
Bob Hansen
Posts: 1517
Joined: Sun Mar 02, 2003 8:15 pm
Location: Salem, NH
Contact:

Post 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.
Hope this was helpful.............good luck,
Bob
User avatar
SteveH
Posts: 327
Joined: Thu Apr 03, 2003 11:37 am
Location: Edinburgh, Scotland
Contact:

Post 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
ben_josephs
Posts: 2456
Joined: Sun Mar 02, 2003 9:22 pm

Post 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
User avatar
Bob Hansen
Posts: 1517
Joined: Sun Mar 02, 2003 8:15 pm
Location: Salem, NH
Contact:

Post by Bob Hansen »

Nice touch ben_josephs. I did not consider multiple instances on the line.
Hope this was helpful.............good luck,
Bob
User avatar
SteveH
Posts: 327
Joined: Thu Apr 03, 2003 11:37 am
Location: Edinburgh, Scotland
Contact:

Post 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.
rlporter5
Posts: 4
Joined: Sun Aug 27, 2006 12:08 am

Thanks!

Post 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
Ed
Posts: 103
Joined: Tue Mar 04, 2003 9:09 am
Location: Devon, UK

Post 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.
Post Reply