|
Using Find and Replace
From NeoWiki
Revision as of 20:02, 3 December 2007 (edit) Sardisson (Talk | contribs) (indent yoxi's macro) ← Previous diff |
Current revision (23:23, 28 December 2010) (edit) (undo) Sardisson (Talk | contribs) (→Changing the style of all occurrences of a string - add sample instructions that also show the use of regexp grouping and replacement, and changing font styles) |
||
(6 intermediate revisions not shown.) | |||
Line 1: | Line 1: | ||
- | OpenOffice.org and thus NeoOffice use a somewhat convoluted Find-and-Replace syntax. This page will hopefully demystify some of the commonly-used commands. | + | OpenOffice.org and thus NeoOffice use a somewhat convoluted Find-and-Replace syntax. This page will hopefully demystify some of the commonly-used commands. For more detailed information, you may want to check the built-in NeoOffice help. In NeoOffice, go the the {{Menu|Help}} menu and choose {{Menu|NeoOffice Help}}. Search for <tt>"replacing"</tt> or <tt>"regular expressions"</tt> (without quotes) in the {{Section|Find}} tab of {{Window|NeoOffice Help}}. |
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | + | ||
===Regular Expressions=== | ===Regular Expressions=== | ||
Line 127: | Line 121: | ||
:*The "'''?"''' character finds zero or one occurrence of the character in front of it. | :*The "'''?"''' character finds zero or one occurrence of the character in front of it. | ||
::"'''ab?c"''' finds "ac" and "abc", but doesn't find "abbc". | ::"'''ab?c"''' finds "ac" and "abc", but doesn't find "abbc". | ||
+ | |||
+ | ===Sample Search to Reformat Numbers=== | ||
+ | <!-- pull the extra explanation and the sample search into the wiki --> | ||
+ | [[User:Jim|Jim]] provides a search that uses these special characters (as well as additional information) [http://trinity.neooffice.org/modules.php?name=Forums&file=viewtopic&t=7153 here]. | ||
==Changing the style of all occurrences of a string == | ==Changing the style of all occurrences of a string == | ||
*To change the character formatting of all occurrences of a string, type that string in the search field and then in the replace field. Click on {{button|More Options}} and then {{button|Format}}. Set the {{Section|Font}} and {{Section|Font Effects}} as desired. | *To change the character formatting of all occurrences of a string, type that string in the search field and then in the replace field. Click on {{button|More Options}} and then {{button|Format}}. Set the {{Section|Font}} and {{Section|Font Effects}} as desired. | ||
*Click on {{button|Replace All}}. | *Click on {{button|Replace All}}. | ||
+ | |||
+ | ===Converting underscore-delimited plain text to italics=== | ||
+ | |||
+ | The above instructions can be expanded upon to change “marked-up plain text†into styled text. For example, it is common in plain-text documents to indicate italics (or underlining) by the use of underscore characters, and you may wish to convert <code>_this should be italics_</code> into <i><code>this should be italics</code></i> in a document. | ||
+ | |||
+ | # Open the {{window|Find & Replace}} window. | ||
+ | # In the {{PrefName|Search for}} box, enter '''<code>_([^_]+)_</code>''' | ||
+ | #: This means: find a leading underscore, then collect any number of characters that are not an underscore, up to a trailing underscore. The characters between the underscores are grouped (by using the parentheses around them). | ||
+ | # In the {{PrefName|Replace with}} box, enter '''<code>$1''' | ||
+ | #: This means: replace the characters found above that were grouped. This will cause NeoOffice to get rid of the leading and trailing underscores when replacing, leaving you with only the actual text. | ||
+ | # With the cursor still in the {{PrefName|Replace with}} box, press the {{button|Format}} button (you may first need to press the {{button|More Options}} button to expand the window to show additional options). | ||
+ | # In the {{window|Text Format(Search)}} window that appears, click on the {{section|Font}} tab, select '''Italic''' from the {{prefName|Typeface}} column, and press the {{button|OK}} button to close that window. | ||
+ | # Check the {{prefName|Regular expressions}} checkbox. | ||
+ | |||
+ | You can press {{button|Replace}} to replace the first instance (to double-check that NeoOffice will do what you think it will do) and, if all is well, press {{button|Replace All}} to replace all instances. If you’ve made a mistake, simply invoke NeoOffice’s undo command. | ||
==Parentheses, etc.== | ==Parentheses, etc.== | ||
Line 147: | Line 160: | ||
:*Do the same with the closing parenthese. | :*Do the same with the closing parenthese. | ||
- | === Sample Search === | + | === Sample Search to Reformat Paragraphs === |
From [[User:amayze|amayze]]: | From [[User:amayze|amayze]]: | ||
Line 166: | Line 179: | ||
==Related External Links== | ==Related External Links== | ||
- | [http://wiki.services.openoffice.org/wiki/Documentation/How_Tos/Regular_Expressions_in_Writer OOo wiki article on Regular Expressions] | + | * Trinity threads on the subject: [http://trinity.neooffice.org/modules.php?name=Forums&file=viewtopic&t=1030], [http://trinity.neooffice.org/modules.php?name=Forums&file=viewtopic&t=1383], [http://trinity.neooffice.org/modules.php?name=Forums&file=viewtopic&t=1417], [http://trinity.neooffice.org/modules.php?name=Forums&file=viewtopic&t=3708] |
+ | * Iannz Find & Replace Macro: http://homepages.paradise.net.nz/hillview/OOo/IannzFindReplace.sxw | ||
+ | * Alternative Find & Replace Extension: http://extensions.services.openoffice.org/node/743 | ||
+ | * [http://sally.neooffice.org/neojava_userguide/userguide/NEO_2_chapter_2_v_0_9.odt Chapter two] of the NeoOffice 2.x User Guide contains useful information about Find and Replace. (Note that this is a direct download link) | ||
+ | * The [http://documentation.openoffice.org/manuals/oooauthors2/0600MG-MigrationGuide.pdf#page=69 Find and Replace] section of the OpenOffice.org 2.x Migration Guide pdf (2.3 MB) also contains a useful chart comparing Find and Replace in Microsoft Office and OpenOffice.org. | ||
+ | *[http://wiki.services.openoffice.org/wiki/Documentation/How_Tos/Regular_Expressions_in_Writer OOo wiki article on Regular Expressions]<br> | ||
+ | *[http://openoffice.blogs.com/openoffice/2008/04/searching-and-r.html Searching and replacing for carriage returns, tabs, and other characters, using regular expressions] by Solveig Haugland | ||
{{botlangbarEN|[[Fr:Trucs pour la recherche et le remplacement|Français]] [[It:Usare_Cerca_e_Sostituisci|Italiano]] [[Es:Consejos_para_usar_buscar_y_reemplazar|Español]]}} | {{botlangbarEN|[[Fr:Trucs pour la recherche et le remplacement|Français]] [[It:Usare_Cerca_e_Sostituisci|Italiano]] [[Es:Consejos_para_usar_buscar_y_reemplazar|Español]]}} | ||
[[Category:Tips and Hints]][[Category:NeoOffice]][[Category:OpenOffice.org]] | [[Category:Tips and Hints]][[Category:NeoOffice]][[Category:OpenOffice.org]] |
Current revision
OpenOffice.org and thus NeoOffice use a somewhat convoluted Find-and-Replace syntax. This page will hopefully demystify some of the commonly-used commands. For more detailed information, you may want to check the built-in NeoOffice help. In NeoOffice, go the the Help menu and choose NeoOffice Help. Search for "replacing" or "regular expressions" (without quotes) in the Find tab of NeoOffice Help.
Contents |
Regular Expressions
- Search and replace of formatting characters, such as returns (paragraph ends) and tabs can only be done by using the regular expression feature. You can't paste these characters into the search and replace fields.
- Click on More Options in the Find & Replace dialog and check the Regular expressions box, then fill the codes required. These are given in the online help, but the explanations are not very clear.
N.B All the regular expressions are given between quotes. These quotes must not be entered in the search and replace fields.
Non Printing Characters
- Line breaks
- To find a line break, type "\n" in the search field.
- If "\n" is entered in the replace field, it means paragraph end.
- To change a line break into a paragraph break, enter "\n" in both search and replace fields.
- Paragraph ends
- To find a paragraph break, type "$" in the search field, be aware that "$^$" does not find adjacent empty paragraphs - it returns no matches. But any text$ finds and selects occurrences of any text at the end of lines, without selecting the end-of-line character itself.
- To delete the empty paragraphs, type "^$" in the search field and leave empty the replace field.
- There is no way in the Find and Replace window to replace two returns; Iannz's Find & Replace Macro will do this.
- contributes the following macro useful for reformatting text where every line ends with a return (plain text to proper paragraphs):
- sub reWrap rem -------------------------------------------------- rem define variables dim document as object dim dispatcher as object rem -------------------------------------------------- rem get access to the document document = ThisComponent.CurrentController.Frame dispatcher = createUnoService("com.sun.star.frame.DispatchHelper") rem -------------------------------------------------- rem dispatcher.executeDispatch(document, ".uno:ExecuteSearch", "", 0, Array()) rem -------------------------------------------------- dim searchArgs(18) as new com.sun.star.beans.PropertyValue searchArgs(0).Name = "SearchItem.StyleFamily" searchArgs(0).Value = 2 searchArgs(1).Name = "SearchItem.CellType" searchArgs(1).Value = 0 searchArgs(2).Name = "SearchItem.RowDirection" searchArgs(2).Value = true searchArgs(3).Name = "SearchItem.AllTables" searchArgs(3).Value = false searchArgs(4).Name = "SearchItem.Backward" searchArgs(4).Value = false searchArgs(5).Name = "SearchItem.Pattern" searchArgs(5).Value = false searchArgs(6).Name = "SearchItem.Content" searchArgs(6).Value = false searchArgs(7).Name = "SearchItem.AsianOptions" searchArgs(7).Value = false searchArgs(8).Name = "SearchItem.AlgorithmType" searchArgs(8).Value = 1 searchArgs(9).Name = "SearchItem.SearchFlags" searchArgs(9).Value = 65536 searchArgs(10).Name = "SearchItem.SearchString" searchArgs(11).Name = "SearchItem.ReplaceString" searchArgs(12).Name = "SearchItem.Locale" searchArgs(12).Value = 255 searchArgs(13).Name = "SearchItem.ChangedChars" searchArgs(13).Value = 2 searchArgs(14).Name = "SearchItem.DeletedChars" searchArgs(14).Value = 2 searchArgs(15).Name = "SearchItem.InsertedChars" searchArgs(15).Value = 2 searchArgs(16).Name = "SearchItem.TransliterateFlags" searchArgs(16).Value = 1280 searchArgs(17).Name = "SearchItem.Command" searchArgs(17).Value = 3 searchArgs(18).Name = "Quiet" searchArgs(18).Value = true searchArgs(10).Value = "$" searchArgs(11).Value = "±" dispatcher.executeDispatch(document, ".uno:ExecuteSearch", "", 0, searchArgs()) searchArgs(10).Value = "±±" searchArgs(11).Value = "$" dispatcher.executeDispatch(document, ".uno:ExecuteSearch", "", 0, searchArgs()) searchArgs(10).Value = "±" searchArgs(11).Value = " " dispatcher.executeDispatch(document, ".uno:ExecuteSearch", "", 0, searchArgs()) end sub
- (from this thread in trinity). For information on how to save this macro code for use, see Saving Code as a Macro.
- Line starts
- To find the first character of a new line (or paragraph), type "^." in the search field.
- Spaces
- To find spaces, enter a space (with the space bar) in the search field (i.e. you don't need to use a regular expression). That finds both unique and multiple spaces.
- To replace multiple spaces with a unique one, however, you do have to use regular expressions. Type " +" (space followed by a + sign), or " *" ( space followed by a * sign), or "[:space:]+" or "[:space:]*" in the search field and type " " (a space) in the replace field.
- To delete the spaces at the end of lines, type " +$" in the search field and leave the replace field empty.
- Tabs
- To find tabs, type "\t" in the search field . It's the same expression in the replace field.
- To insert a tab at the beginning of a line, type "^." in the search field and "\t&" in the replace field.
- To insert a tab at the beginning of each paragraph, type "$" in the search field and "\n\ t" in the replace field.
Special characters
- The "." (dot).
- That character means any character except a line or a paragraph end.
- The "&" character.
- That character means "the string which has been found with the search criteria you used". For example, type "test" in the search field and "new &" in the replace field. The string "test" is replaced with "new test" in the document.
- If you want to get the "&" character in your text, you have to type a backslash "\" in front of it.
- For example, if you want to replace "John & Jack" with "Peter & Paul", you must enter "John & Jack" in the search field and "Peter \& Paul" in the replace field.
- The quantifiers "*", "+", and "?".
- The "*" character finds zero or more occurrences of the character in front of it.
- "ab*c" finds "ac", "abc", "abbc", etc.
- The "+" character finds one or more occurrences of the character in front of it.
- "ab+c" finds "abc", "abbc", etc.
- The "?" character finds zero or one occurrence of the character in front of it.
- "ab?c" finds "ac" and "abc", but doesn't find "abbc".
- The "*" character finds zero or more occurrences of the character in front of it.
Sample Search to Reformat Numbers
provides a search that uses these special characters (as well as additional information) here.
Changing the style of all occurrences of a string
- To change the character formatting of all occurrences of a string, type that string in the search field and then in the replace field. Click on More Options and then Format. Set the Font and Font Effects as desired.
- Click on Replace All.
Converting underscore-delimited plain text to italics
The above instructions can be expanded upon to change “marked-up plain text†into styled text. For example, it is common in plain-text documents to indicate italics (or underlining) by the use of underscore characters, and you may wish to convert _this should be italics_
into this should be italics
in a document.
- Open the Find & Replace window.
- In the Search for box, enter
_([^_]+)_
- This means: find a leading underscore, then collect any number of characters that are not an underscore, up to a trailing underscore. The characters between the underscores are grouped (by using the parentheses around them).
- In the Replace with box, enter
$1
- This means: replace the characters found above that were grouped. This will cause NeoOffice to get rid of the leading and trailing underscores when replacing, leaving you with only the actual text.
- With the cursor still in the Replace with box, press the Format button (you may first need to press the More Options button to expand the window to show additional options).
- In the Text Format(Search) window that appears, click on the Font tab, select Italic from the Typeface column, and press the OK button to close that window.
- Check the Regular expressions checkbox.
You can press Replace to replace the first instance (to double-check that NeoOffice will do what you think it will do) and, if all is well, press Replace All to replace all instances. If you’ve made a mistake, simply invoke NeoOffice’s undo command.
Parentheses, etc.
- To find expressions in parentheses, including the parentheses, you can enter "\([a-z 0-9]*\)"
You will get a problem with the non ASCII characters, such as accented letters or ligatures in some languages. To solve that problem, enter "\([^\)]+\)" in the search field. This expression means : an opening parenthese followed by one or more characters, except a closing parenthese, followed by a closing parenthese.
- To find the strings in braces or square brackets, proceed in the same way:
type "\{[^\}]+\}" or "\[[^\]]+\]" in the search field.
- To italicize a string in parentheses, leaving the parentheses in regular style, follow these steps:
- Find the strings in parentheses, as described above.
- Press Cmd-I.
- Click on Find All.
- Type "\(" in the search field, click on Format in More Options. Choose the font and typeface: Italic.
- Type "(" in the replace field, specifying the font and typeface: No italic.
- Click on Replace All.
- Do the same with the closing parenthese.
Sample Search to Reformat Paragraphs
From amayze:
I recently complained in this post about the fact that NeoOffice regular expressions sometimes mean two different things depending where you use them, however this [in a Word .doc, "paragraph breaks" were actually represented in the file as line breaks within one giant paragraph (#$*&!@ Word...), so it's no wonder you ended up with one big mass of text after trying to strip out the whitespace] is exactly the instance when this is useful.
Try doing a search and replace.
- Click on "More Options..." and check the box by "Regular Expressions"
- Search for "\n" - which will match the line breaks in your document - and replace it with "\n" which will insert paragraph breaks in their place.
- Now change the search term to "^$" and the replace to "" (nothing) and you will be able to remove of the blank paragraphs in between your text.
Hope that helps.[1]
Behavior of the arrows at the end of the vertical scrollbar
The vertical scrollbar ends with three little icons that are, from top to bottom: two litte black triangles pointing up, a small blue ball and two little black triangles pointing down. Usually icons with the double triangles are used to go to Previous Page and to go to Next page
When you use Search and Replace, the two icons take different meanings: icon with triangles pointing up mean Continue search backwards, while icon with triangles pointing down means Continue search forward.
To go back to the normal behavior of these two icons you have to click on the icon representing the small blue ball; it will show a window called Navigation and there you have to click on Page icon.
Related External Links
- Trinity threads on the subject: [2], [3], [4], [5]
- Iannz Find & Replace Macro: http://homepages.paradise.net.nz/hillview/OOo/IannzFindReplace.sxw
- Alternative Find & Replace Extension: http://extensions.services.openoffice.org/node/743
- Chapter two of the NeoOffice 2.x User Guide contains useful information about Find and Replace. (Note that this is a direct download link)
- The Find and Replace section of the OpenOffice.org 2.x Migration Guide pdf (2.3 MB) also contains a useful chart comparing Find and Replace in Microsoft Office and OpenOffice.org.
- OOo wiki article on Regular Expressions
- Searching and replacing for carriage returns, tabs, and other characters, using regular expressions by Solveig Haugland