Using Find and Replace

From NeoWiki

Revision as of 23:23, 28 December 2010 by Sardisson (Talk | contribs)
(diff) ←Older revision | Current revision (diff) | Newer revision→ (diff)
Jump to: navigation, search

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".

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.

  1. Open the Find & Replace window.
  2. 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).
  3. 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.
  4. 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).
  5. 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.
  6. 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.

  1. Click on "More Options..." and check the box by "Regular Expressions"
  2. 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.
  3. 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


This article in other languages: Français Italiano Español
Personal tools