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?
Can TP b taught 2 find text between parenthesis & remove
Moderators: AmigoJack, bbadmin, helios, Bob Hansen, MudGuard
- Bob Hansen
- Posts: 1516
- Joined: Sun Mar 02, 2003 8:15 pm
- Location: Salem, NH
- Contact:
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.
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
Bob
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:
Note to self: Must print out TP Regex help file for use on Mac
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
\(.*?\)
-
- Posts: 2461
- Joined: Sun Mar 02, 2003 9:22 pm
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,
[^)] 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:
\( matches a literal open parenthesis symbolFind what: \([^)]*\)
Replace with: [nothing]
[X] Regular expression
[^)] 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
- Bob Hansen
- Posts: 1516
- Joined: Sun Mar 02, 2003 8:15 pm
- Location: Salem, NH
- Contact:
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.you can achieve the same effect by not allowing any close parenthesis symbols (or not allowing any open parenthesis symbols) within the parenthesised text