Find and Replace

www.altova.com Print this Topic Previous Page Up One Level Next page

Home >  User Guide and Reference > Editing Views > Text View >

Find and Replace

In this section:

 

Searching for text in a document
Find options
Replacing text
Using regular expressions
Regular expression examples

 

Searching for text in a document

Press Ctrl+F (or select the menu command Edit | Find) to display the Find dialog (screenshot below, top right). You can then search in the entire document or within a text selection for a search term that you enter in the dialog.

 

Enter a string to find, or use the combo box to select a string from one of the last 10 strings.
When you enter or select a string to find, all matches are highlighted and the positions of the matches are indicated by beige markers in the scroll bar (see screenshot).
The currently selected match has a different highlight color than the other matches, and its position is indicated in the scroll bar by the dark blue cursor-marker.
The total number of matches is listed below the search term field, together with the index position of the currently selected match (see screenshot below). For example, 2 of 4 indicates that the second of four matches is currently selected.
You can move from one match to the next, in both directions, by selecting the Previous (Shift+F3) and Next (F3) buttons at bottom right.

TextViewFind

To switch between the Find and Replace modes, click the down-arrow button at top left.
To close the dialog click the Close button at top right.

 

Note the following points:

 

The Find dialog is modeless. This means that it can remain open while you continue to use Text View.
If text is selected prior to opening the dialog box, then the selected text is automatically inserted into the search term field (aka Find field).
To search within a selection, do the following: (i) Mark the selection; (ii) Toggle on the Find in Selection option to lock the selection; (iii) Enter the search term. To search within another selection, unlock the current selection by toggling off  the Find in Selection option, then make the new selection and toggle on the Find in Selection option.
After the Find dialog is closed, you can repeat the current search by pressing F3 for a forward search, or Shift+F3 for a backward search. The Find dialog will appear again in this case.

 

Find options

Options to determine Find criteria can be specified via buttons located below the search term field (see screenshot below). When an option is toggled on, its button color changes to blue (see the first (casing) option in the screenshot below).

TextViewFindDialog

You can select from the following options:

 

Match case: Case-sensitive search when toggled on (Address is not the same as address).
Match whole word: Only the exact words in the text will be matched. For example, for the input string fit, with Match whole word toggled on, only the word fit will match the search string; the fit in fitness, for example, will not be matched.
Regular expression: If toggeld on, the search term will be read as a regular expression. See Regular expressions below for a description of how regular expressions are used.
Filter results: Select one or more document components where the search is to be carried out.
Find anchor: When a search term is entered, the matches in the document are highlighted and one of these matches will be marked as the current selection. The Find anchor toggle determines whether that first current selection is made relative to the cursor position or not. If Find anchor is toggled on, then the first currently selected match will be the next match from the current cursor location. If Find anchor is toggled off, then the first currently selected match will be the first match in the document, starting from the top.
Find in selection: When toggled on, locks the current text selection and restricts the search to the selection. Otherwise, the entire document is searched. Before selecting a new range of text, unlock the currently selection by toggling off  the Find in Selection option.

 

Replacing text

To replace a text string, do the following:

 

1.Press Ctrl+H (or select the menu command Edit | Replace) to open the Replace dialog (screenshot below). (Alternatively, you can switch to Replace mode of the Find/Replace dialog by clicking the down-arrow button at the top left of the dialog.)

TextViewReplace

2.Enter the string to be replaced in the Find field, and enter the new string in the Replace field. The number of text matches to replace and the index of the currently selected match is displayed below the Replace field. Also, the locations of matches are indicated in the scroll bar by beige markers (see Searching for text in a document above for more information). For example, the screenshot above shows that there are five text matches for the string type, and that the third of these matches is currently selected.
3.The Replace Next and Replace All buttons are located to the right of the Replace field. If you click Replace Next, one of the following happens: (i) If the cursor is located adjacent to a match or inside a match, then the match is replaced; (ii) if the cursor is located outside a match, it jumps to the next match; click Replace Next to replace this match. Click Replace All to replace all matches.

 

Note the following points:

 

To replace text within a selection—rather than the entire document—do the following: (i) Mark the selection; (ii) Toggle on the Find in Selection option to lock the selection; (iii) Enter the Find and Replace text strings; (iii) Click Replace Next or Replace All as required. To replace text within another selection, unlock the current selection by toggling off  the Find in Selection option, then make the new selection and toggle on the Find in Selection option to lock the new selection.
To undo a replace action, press Ctrl+Z or select Edit | Undo.

 

Using regular expressions

You can use regular expressions (regex) to find a text string. To do this, first, switch the Regular expression option on (see Find options above). This specifies that the text in the search term field is to be evaluated as a regular expression. Next, enter the regular expression in the search term field. For help with building a regular expression, click the Regular Expression Builder button, which is located to the right of the search term field (see screenshot below). Click an item in the Builder to enter the corresponding regex metacharacter/s in the search term field. The screenshot below shows a simple regular expression to find email addresses. For a brief description of metacharacters, see the section Regular expression metacharacters below.

Click to expand/collapse

Regular expression metacharacters

Given below is a list of regular expression metacharacters.

 

.

Matches any character. This is a placeholder for a single character.

(

Marks the start of a tagged expression.

)

Marks the end of a tagged expression.

(abc)

The ( and ) metacharacters mark the start and end of a tagged expression. Tagged expressions may be useful when you need to tag ("remember") a matched region for the purpose of referring to it later (back-reference). Up to nine expressions can be tagged (and then back-referenced later, either in the Find or Replace field).

 

For example, (the) \1 matches the string the the. This expression can be literally explained as follows: match the string "the" (and remember it as a tagged region), followed by a space character, followed by a back-reference to the tagged region matched previously.

\n

Where n is a variable that can take integer values from 1 through 9. The expression refers to the first through ninth tagged region when replacing. For example, if the find string is Fred([1-9])XXX and the replace string is Sam\1YYY, this means that in the find string there is one tagged expression that is (implicitly) indexed with the number 1; in the replace string, the tagged expression is referenced with \1. If the find-replace command is applied to Fred2XXX, it would generate Sam2YYY.

\<

Matches the start of a word.

\>

Matches the end of a word.

\x

Allows you to use a character x, which would otherwise have a special meaning. For example, \[ would be interpreted as [ and not as the start of a character set.

[...]

Indicates a set of characters. For example, [abc] means any of the characters a, b or c. You can also use ranges: for example [a-z] for any lower case character.

[^...]

The complement of the characters in the set. For example, [^A-Za-z] means any character except an alphabetic character.

^

Matches the start of a line (unless used inside a set, see above).

$

Matches the end of a line. Example: A+$ to find one or more A's at end of line.

*

Matches 0 or more times. For example, Sa*m matches Sm, Sam, Saam, Saaam and so on.

+

Matches 1 or more times. For example, Sa+m matches Sam, Saam, Saaam and so on.

 

 

Representation of special characters

Note the following expressions.

 

\r

Carriage Return (CR). You can use either CR (\r) or LF (\n) to find or create a new line

\n

Line Feed (LF). You can use either CR (\r) or LF (\n) to find or create a new line

\t

Tab character

\\

Use this to escape characters that appear in regex expression, for example: \\\n

 

Regular expression examples

This example illustrates how to find and replace text using regular expressions. In many cases, finding and replacing text is straightforward and does not require regular expressions at all. However, there may be instances where you need to manipulate text in a way that cannot be done with a standard find and replace operation. Consider, for example, that you have an XML file of several thousand lines where you need to rename certain elements in one operation, without affecting the content enclosed within them. Another example: you need to change the order of multiple attributes of an element. This is where regular expressions can help you, by eliminating a lot of work which would otherwise need to be done manually.

 

Example 1: Renaming elements

The sample XML code listing below contains a list of books. Let's suppose your goal is to replace the <Category> element of each book to <Genre>. One of the ways to achieve this goal is by using regular expressions.

 

<?xml version="1.0" encoding="UTF-8"?>
<books xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="books.xsd">
  <book id="1">
    <author>Mark Twain</author>
    <title>The Adventures of Tom Sawyer</title>
    <category>Fiction</category>
    <year>1876</year>
  </book>
  <book id="2">
    <author>Franz Kafka</author>
    <title>The Metamorphosis</title>
    <category>Fiction</category>
    <year>1912</year>
  </book>
  <book id="3">
    <author>Herman Melville</author>
    <title>Moby Dick</title>
    <category>Fiction</category>
    <year>1851</year>
  </book>
</books>

 

To solve the requirement, follow the steps below:

 

1.Press Ctrl+H to open the Find and Replace dialog box.
2.Click Use regular expressions _ic_find_regex.
3.In the Find field, enter the following text: <category>(.+)</category> . This regular expression matches all category elements, and they become highlighted.

inc-RegexExample01

To match the inner text of each element (which is not known in advance), we used the tagged expression (.+) . The tagged expression (.+) means "match one or more occurrences of any character, that is .+ , and remember this match". As shown in the next step, we will need the reference to the tagged expression later.

 

4.In the Replace field, enter the following text: <genre>\1</genre> . This regular expression defines the replacement text. Notice it uses a back-reference \1 to the previously tagged expression from the Find field. In other words, \1 in this context means "the inner text of the currently matched <category> element".
5.Click Replace All _ic_regex_replaceall and observe the results. All category elements have now been renamed to genre, which was the intended goal.

 

Example 2: Changing the order of attributes

The sample XML code listing below contains a list of products. Each product element has two attributes: id and a size. Let's suppose your goal is to change the order of id and size attributes in each product element (in other words, the size attribute should come before id). One of the ways to solve this requirement is by using regular expressions.

 

<?xml version="1.0" encoding="UTF-8"?>
<products xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="products.xsd">
  <product id="1" size="10"/>
  <product id="2" size="20"/>
  <product id="3" size="30"/>
  <product id="4" size="40"/>
  <product id="5" size="50"/>
  <product id="6" size="60"/>
</products>

 

To solve the requirement, follow the steps below:

 

1.Press Ctrl+H to open the Find and Replace dialog box.
2.Click Use regular expressions _ic_find_regex.
3.In the Find field, enter the following: <product id="(.+)" size="(.+)"/> . This regular expression matches a product element in the XML document. Notice that, in order to match the value of each attribute (which is not known in advance), a tagged expression (.+) is used twice. The tagged expression (.+) matches the value of each attribute (assumed to be one or more occurrences of any character, that is .+ ).
4.In the Replace field, enter the following: <product size="\2" id="\1"/> . This regular expression contains the replacement text for each matched product element. Notice that it uses two references \1 and \2 . These correspond to the tagged expressions from the Find field. In other words, \1 means "the value of attribute id" and \2 means "the value of attribute size".

inc-RegexExample02

6.Click Replace All _ic_regex_replaceall and observe the results. All product elements have now been updated so that attribute size comes before attribute id.

 


© 2019 Altova GmbH