Bottom     Contents

StrongED

Search and Replace

Introduction

StrongED has a very powerful search and replace syntax which is used throughout the program. However, it is somewhat unconventional and will repay some careful study.

Nearly all aspects of the use of StrongED can be customised, principally in the mode definition files. The BaseMode file defines the defaults, which includes how the search and replace interface operates. It is extremely unlikely that any one would want to change this, but be aware that it is not hard coded.

StrongED is so flexible that in many cases there will usually be more than one way of achieving the same result.Introduction

Simple Searches

This section describes the basic interface to the various search operations available. The next sections deals with the advanced search syntax.

Interactive Search

To find an occurrence of an item of text that you cannot spot, or is off the screen in a long document, position the caret in the document where you want to search from. Remember that if you are working on a document the caret will not usually be at the start.

Press <f4> to pop up the Interactive Search dialogue box, which appears by default in the top right corner of the document window. This can be changed in 'Global Choices - Windows'.

Fig 1 - Interactive Search

Start to type the text you want to find. Immediately the first character appears in the icon, the next occurrence of that character in the document is highlighted, by default in red. As you type more characters, the highlight moves to the next whole match, and if you delete characters then earlier matches are re-found. The text you type can include spaces but not control characters, such as newline or tab.

You can edit the text in the icon in the usual way, moving the caret using the left/right cursor keys or by positioning the pointer and clicking. The document highlight moves interactively to reflect the changes you make. If you type text that cannot be matched, the highlight moves to match as many characters from the beginning as possible.

By default the search is not case-sensitive, so "the" will also match "The", "THE" and "tHe". If you click on the 'Case' option button then only an exact match will be found. 'Case' can also be toggled using <ctrl-C>.

Repeat searches

Once you have typed the whole text to find, you can search for further matches by clicking on the 'Next' button to look forward in the document, or on the 'Prev' (previous) button to look backward. This will search back beyond the initial search start position if required. 'Next' and 'Prev' searches can also be done by <cursor up/down> or by <ctrl-N> and <ctrl-P>.

To close the dialogue box press Escape. The caret will be positioned before the last highlight, which will disappear when you start editing again. The dialogue box also disappears if you click on the document to position the caret yourself.

Note that there is no way to return directly to the original search start position, so in a long document you may want to place a marker before starting a search.

Search history

You may want to repeat a search after you have closed the dialogue box. To recall an earlier search text press <f4> after the dialogue box is opened. Repeat pressing <f4> to cycle through all the earlier searches since StrongED was loaded.

If there have been a lot of searches this may be awkward. To select one particular search text, position the pointer over the text icon, where it changes to a menu pointer. Click <menu> to open a 'History' menu of all earlier search texts, and choose the one you want.

You can also move through the history by pressing <pageup> or <pagedown> or by clicking with <select> or <adjust> on the 'Old' button.

Search/Replace

To do a conventional document search, with the option to replace what is found, press <shift-f4> to pop up the Search/Replace dialogue box:

Fig 2 - Search/Replace

The 'Search' and 'Replace' text icons have the same characteristics as the one in Interactive Search, and the 'Search' icon shares its search history with it. This means that you can identify that a search text exists, or is correct, with the Interactive Search and then do a Search/Replace without typing it again. You can configure StrongED to automatically recall the last history items in 'Global Choices - Edit'.

The text in the 'Replace' icon is used to replace (surprise) document text that matches the 'Search' text.

The 'History' menu and <pageup> and <pagedown> operate in the same way on each text icon as in the Interactive Search. <shift-f4> acts as <f4> did, except that it recalls the history of both icons, as does the 'Old' button.

The search can be controlled in the same way by 'Case' option button. The 'Line' option button also restricts a search to only the start of lines, and it can be toggled by <ctrl-L>. The 'Advanced' option button will be ignored for now.

Repeat replace

To start a search, click on the 'GO' button, or press <return> with the caret in the 'Replace' icon. StrongED searches through the document from the caret position until it finds a complete match for the search text. This is highlighted and a What now? dialogue box appears in place of Search/Replace:

Fig 3 - What now?

The search actions available now are the three buttons on the left. 'Undo' and 'Redo' have identical actions to those items on the StrongED>Edit menu and they may also be done using <f8> and <ctrl-f8>.

If the highlighted text is not the one that was wanted, or should not be replaced, click on the 'Next' button or press <ctrl-N> or press <cursor down>. StrongED will carry on searching until it finds another match.

If the highlighted text should be replaced, then click on the 'Replace' button or press <return>. The replacement is done and StrongED finds another match. You will often not see the replacement happen, because the document scrolls up.

You can continue to click either 'Next' or 'Replace' until the last match in the document, at which point the dialogue box closes. At any time you can press Escape to end the search. The document caret is left at the position of the last match.

Replace all

If you are confident that the search and replace texts are correct and that all occurrences are to be replaced, click on the 'replace All' button, or press <ctrl-A>. This will do as it says, but it will not highlight any further matches. On completion, the dialogue box is closed, a transient report appears saying how many items were replaced, and the document caret stays at the position of the first match.

Note that it is very easy to ruin your document by ill-advised use of 'replace All'. For example, you may want to replace all occurrences of "Bill" by "William", so you set up the Search/Replace with these texts, click on 'GO' and then on 'replace All'. Unfortunately you will have also replaced the word "bill" by "William" and "billow" will have become "Williamow". You have probably seen the effect of such unfortunate searches in published documents sometimes.

'Undo', press <f8>, is your friend. It will undo the whole of a 'replace All' at one go, and undo each individual 'Replace' in turn. However, 'Undo' only acts on the current document, so you will have more problems if you make such a mistake with 'replace All' on multiple documents (see later).

Restricting a search by using the 'Case' and 'Line' options can often resolve such problems, but a handy hint is to enlarge the search text to include preceding or succeeding spaces, or even words. For example, by searching for "Bill " and replacing with "William " you would avoid matching words beginning with "bill".

Counting

To only count how many occurrences of a particular text are in your document, click on the 'couNt' button or press <ctrl-N>. After a short pause, a transient report appears saying how many were found. Remember to put the caret at the start of the document if you want to count them all.

StrongED does not have a word count available from this dialogue box. Follow the main menu to StrongED>Misc>File info>, or press <ctrl-I>, to get to it.

Search scope

The icon in the bottom left of the Search/Replace dialogue box is ambiguously labelled 'Search', and should, perhaps, be better labelled 'Scope'. It also has a depressed border, which should indicate that the contents are only displaying information, but it is nevertheless an active button.

By default the 'Scope' icon shows 'Text', which means that any searches are done only on the current document, the one with the caret. Click on 'Text', or press <ctrl-S>, and it changes to 'All'. Searches, and replacements, will now be done on all documents opened by StrongED, including iconised and hidden ones.

This is very powerful, but like all powerful tools it allows you to do a lot of damage if used carelessly. Open the List of Texts window before using this, to check what might be changed. This also allows you to save documents that have been changed immediately.

[Toggle the List of Texts permanently by clicking on the second icon at the top of the document window, or press <ctrl-L>. Open it transiently by pressing <shift-cursor up/down>.]

If a block is marked in the current document there is an additional 'Block' option for the 'Scope' icon. This will also be the default when the Search/Replace dialogue box is opened and the caret is in the marked block. Its effect is to restrict searches to within the marked block.

A particular use for this is in checking that a replacement is working correctly, particularly with Advanced searches. Remember, that once a replacement has been done, the search will highlight the next match, which may well move the last one out of view, and you cannot see that it did what you wanted. If you mark a small block around a known match, you will not loose sight of the result. If is correct, you can go on to do the rest without the marked block.

List of Found

This is one of a few 'List of' facilities that may also be part of a function in a modefile, such as the list of PROCs and FNs in BASIC. It is very like the Search part of the Search/Replace, but displays all its matches in a window rather than just highlighting them in the document.

Press <f2> to pop up the 'List of Found' dialogue box:

Fig 4 - List of found

Refer back to Search/Replace for the details of how the 'Line', 'Case', 'Old', 'GO', and the 'Search/Scope' icons work. The 'Advanced' option button will be ignored for now. The associated key presses are the same too, except that now <f2> recalls the recent history of searches.

Click on the 'Display' icon, where it shows 'Whole', to cycle through the other display types: 'From', 'After', and 'Align'.

The display type 'Whole' shows the whole of each line in which there is a match; 'From' shows the line from the match and anything that follows it; 'After' only shows anything that is after the match; 'Align' shows the whole line but aligns the matched texts in a column. A little experimenting makes this clear.

List of Found Display

When you enter a search text and click on 'GO', StrongED does a search for all matches. This will be through the marked block, current text or all texts, according to the 'Scope' set. On completion, it opens a window showing each line in which there is a match, formatted according to the display type.

Fig 5 - List of Found window

This is a coloured display, but you should be able to see that the word "text" is marked bold. The top line, the currently selected match, also has the other words in blue.

If you click on one of the lines, that matched text is highlighted in the document and the caret moves to it. So this is a quick way to move directly to a match.

Click <menu> over the window to open the 'ListOfFound' menu. From here you can save the whole contents of the list, toggle a sort of the lines into alphabetical order, and toggle marks in the document. These are permanent highlights of all the matches, that remain as long as the List Of Found window is open. Lastly, you can change the display type.

List of Word

A very similar facility to the List of Found is defined in the BaseMode, using the function 'ListOfWord'.

Press <ctrl-F> and a List of Found window opens showing all the matches to the current word in the current document, the one with the caret. Press <shift-ctrl-F> and the matches in all documents are shown.

Search speed

StrongED searches documents very quickly, which is why Interactive Search works effectively instantly. If a replacement text is the same length as the matched text then that is also very fast. However, if the replacement text is longer or shorter than the matched text, the file is very large (several Mb) and there a many matches then there can be frustrating delays.

The explanation for this is that a large amount of text has to be moved in memory to open or close the gap each time the length of a word is changed. It is interesting that Edit does not suffer from this problem significantly because it does not handle text in a continuous block, but has a gap around the current operation. This has some penalties elsewhere.

There is little to be done about this except to avoid very large files. This may not be quite so impractical as it sounds. I developed a habit of combining small files into one so that I could do Search and Replace of the whole lot using Edit, which cannot handle multiple files. It was not until quite recently that I realised that this was not the best thing to do with StrongED, and that it is very much quicker to keep the files separate and use the 'All' option instead.

Advanced Searches

The previous section only dealt with search and replace of plain text. Several aspects of RISC OS, and most applications that provide searches, allow the use of wildcards within the text. For example, in the Filer '#' can match any single character and '*' can match zero or more characters. These would allow a search to match the names "Smith", "Smithe", "Smithson", "Smyth", and "Smythe" by searching for "Sm#th*". StrongED does not have such a straightforward way of enhancing searches, but it does have a powerful Advanced Search.

Advanced Search dialogues

Press <shift-f4> to pop up the Search/Replace dialogue box and click on the 'Advanced' option button, or press <ctrl-A> to show its advanced options:

Fig 6 - Advanced Search/Repace

Similarly you can press <f2> to pop up the List of Found dialogue box and click on the 'Advanced' option button to show its advanced search options:

Fig 7 - Advanced List of Found

Expressions

The writeable icons do not contain text, but contain expressions. A search expression is a pattern used to test against the text in a document to find possible matches, and a replace expressions is evaluated to determine what is to be inserted in the text.

The following section is exclusively concerned with how advanced searches work, so at this stage the 'Replace' icon should only contain strings, including the newline string.

Advanced Searching

A search expression is constructed from a sequence of elements, which may be strings of actual text, sets of alternative characters or operators. Spaces can be used to separate elements, but have no other effect. Any possible character may also be entered as what is called an escape sequence.

StrongED does a search by moving through the document a character at a time. At each character a test is made to see if the pattern of the search expression can be matched against the text which follows in every detail.

It does this by testing each element in turn, taking account of elements grouped within ( ). If a match is found, the next element is tested against the text following the text matched so far, and so on.

If each element can be matched, the whole match is highlighted and the 'What now?' dialogue box waits for a decision. If any element does not match, the search moves on to the next character and tries again.

Elements

The the bottom of the expanded dialogue boxes has 22 new buttons showing many of the types of elements. If you move the pointer over these, a brief summary of their use is shown in the bottom icon. If you click on one of the buttons, the element will be added to the 'Search' icon and the caret may be moved to a convenient position, e.g.: between a pair of braces. You can, of course, type elements.

The following paragraphs describe all the elements and explains how they may be used. There is also a descriptive word or phrase, because it often helps to speak out an expression to understand how it will work.

Strings

A string matches a sequence of characters.

"aaaa"

(String)

Actual text is entered as a string of characters between double quotation marks. e.g. "This is some text". It matches exactly what is in it, but does obey the 'Case' option.

Unlike the Simple searches, a string may contain control characters entered as escape sequences (see below).

$ NL

(Newline)

Matches the current newline string. This is a combination of linefeed and carriage return characters, and is shown at the right hand end of the infobar.

CW CursorWord

Matches the word in the document that contained the caret before the search.

&xx &xxxx
&xxxxxxxx

(Byte, Halfword, Word)

Matches the byte, half-word or word represented by a hexadecimal number of 2, 4 or 8 digits. Note that the multiple characters will be in reverse order in the document.

This is most suited to '!Dump' mode, but it seems to bugged there.

Escape sequences

These match single characaters and may be inclucded in strings or sets. A backslash character, '\' starts an escape sequence.

\\

(Backslash)

The '\' character itself must be entered as '\\'.

\xXX

(Hexcode)

A character specified by a hexadecimal number, XX. e.g. \x41 is the character 'A'; \x09 is the tab character.

\cX

(Control)

A control character specified by alphabetical position. e.g. \cJ is character 10, a linefeed.

Note that this is case sensitive, and because of the way it works, is not actually restricted to the alphabet. e.g. \c@ is the null character, \cj is '*'.

\t

(Tab)

The tab character, control code 9.

\n

This should represent a newline character, but a probable bug in StrongED actually matches this to 'n' in version 4.60.

Sets

A set matches any one of the characters included in it.

'abcd'

(Set)

A set is entered between single quotation marks. e.g. 'aeiou' is the set of vowels and matches any vowel in a document.

-

(Range)

Within a set, a range of characters can be specified by entering the first and last characters separated by '-'. e.g. 'A-G' is the set of musical notes and is equivalent to 'ABCDEFG', but also to 'ADGECFB' etc.

Pre-defined sets

To simplify searches, a number of regularly used sets are already defined and named within StrongED. There are alternative names for all of these sets.

. Any IsAny

Matches any single character except a newline. This is equivalent to a wildcard '#'.

? A Alpha IsAlpha

Matches any letter of the alphabet, upper or lower case, plus accented versions, ligatures, diphthongs and foreign characters.

# D Digit IsDigit

Matches any numeral, 0 to 9.

AD AlphaNum IsAlphaNum

Matches any member of the A or D sets, an alphanumeric character. Note that AD is not the same as A D.

Ctrl IsCtrl

Matches any control character, including code 127.

Punct IsPunct

Matches any punctuation character, which is roughly any character that is not AlphaNum, Ctrl or a space.

X Hex IsHex

Matches any hexadecimal numeral, 0-9 plus A-F and a-f.

Upper IsUpper

Matches any upper case member of Alpha.

Lower IsLower

Matches any lower case member of Alpha.

White IsWhite

Matches the whitespace characters: space, tab and the newline string.

In addition to the built-in sets, any modefile can define further named sets in the form of search expressions.

Operators

Operators cobine other elements.

*

(Next in line)

Matches any number of characters up to a match to the following element, as long as it is on the same line.

This is not equivalent to a wildcard '*'. (see 'Zero or more' below)

**

(Next in text)

Matches any number of characters up to a match to the following element anywhere in the document.

These operators might also be described thus: "scan forward through the document to find a match to the next element in the expression ignoring everything in between".

The following element must also be a simple one, that is not a multiple match, and it must exist.

( )

(Group)

Used to group elements to resolve ambiguities. See 'OR' for an example.

[ ]

(Option)

Used to enclose a group of optional elements.

Tries to match the enclosed elements completely to the document, but if no match is found they are ignored and the search continues with the next element, at the same position.

"A"["B"]"C" 	matches "ABC" or "AC"
{ }

(Zero or more)

Used to enclose a group of elements to be matched zero or more times.

Matches any number of consecutive matches of the enclosed elements in the document. If there is no match the search continues with the next element.

"A" matches "A", "AB", "ABB" etc.

A true equivalent of a wildcard '*' is {.}. i.e. match zero or more of any characters.

{ }+

(One or more)

Used to enclose a group of elements to be matched one or more times.

Matches any number of consecutive matches of the enclosed elements in the document. There must be at least one match.

"A"+ matches "AB", "ABB" etc, but not "A" alone.

{ }n:m

(Times)

{ ]n is shorthand for { }n:n.

Used to enclose a group of elements repeated between n and m times.

Matches at least n consecutive matches of the enclosed elements in the document, but there must not be more than m.

|

(OR)

Used to separate elements so that either the first or the next must match.

Matches the first element if possible. If there is no match it is ignored and the next element is tried, and so on. There must be a match.

"A"|"B" matches "A" or "B"

"A"|"B"|"C" matches "A" or "B" or "C"

Sometimes there may be ambiguity with 'OR' which can be resolved by making a 'Group'.

"A"|"B""C" matches "AC" or "BC"

"A"|("B""C") matches "A" or "BC"

~

(NOT)

Used to exclude an element from the search.

Matches only if the next element does not match.

"A"~"B" matches "A" but only if the next character is not "B"

The search position remains at the character after the last true match, so:

"A"~"B"? matches "A" followed by any other letter, but only if it is not "B"

&xx=yy

(Mask)

Matches any byte in the document if the result of masking it with &xx is equal to &yy.

A masking operation is a bitwise AND, so this can be used to search for particular bit patterns.

This is most suited to '!Dump' mode, but it seems to bugged there.

<

(Start)

Matches the start of a line, but it does not include the newline.

>

(End)

Matches the end of a line, but it does not include the newline.

Start and End are of zero length.

Examples

Describing the elements may help you understand what goes on, but it may not help you use Advanced search. Much of the power of the search syntax is that it is used throughout StrongED, not just in the Search/Replace dialogue box. In particular it is used in the Search section of mode files, from which some of the examples are drawn. It is tricky to give many realistic examples of replacements until the use of 'Marks' has been described later.

Text tidying

Many text files can be poorly typed, or have spaces to achieve layout, making them unsuitable for importing into DTP programs, for example. You may want to tidy them up. Note that these would not all be used at the same time.

Remove multiple spaces
Search:  {" "}+
Replace: " "

Replace one or more spaces by a space.

Introduce tabs
Search:  "  "+
Replace: "\t"

Replace two or more spaces by a tab.

Note that the "\t" must be in a string.

Close to the left margin
Search:  {$}+
Replace: $

Replace spaces after a newline with just a newline.

Remove multiple line breaks
Search:  ${$}+
Replace: $

Replace two or more newlines with just one newline.

Mode file searches

The following examples illustrate how the search syntax may be used in for named searches in different mode files.

Find a DEF in BASIC.
"DEF" {" "} "PROC"|"FN" A {AD}

Match "DEF" followed by zero or more spaces, then either "PROC" or "FN" with a letter, which may be followed by any number of letters or digits.

Find a whole comment in C
"/*" ** "*/"
Find a URL in HTML
{AD}+ ":/" { ["~"] AD | '/_.' }+

Match one or more alphanumerics followed by ":/" then 1 or more repeats of (an optional "~", followed by an alphanumeric or one of '/_.') .

Find section start in StrongMode
$~("end"|"#") {A}+

Match the start of a line that does not begin with either "end" or "#" and is immediately followed by 1 or more more letters.

Advanced replace

Although the advanced search syntax is powerful, replacements have so far been only strings, which is limiting. Advanced replacement allow you to include parts of the text that has been matched in the text that replaces it.

Marks

Marks are elements of a search expression that do not affect the pattern that is matched. They are used to subdivide the expression, and hence the matched text.

You can use ten marks, from @0 to @9. If you do not place them elsewhere, @0 is assumed to be at the start of the expression and @9 at the end.

Ranges

Ranges are elements of a replace expression that specify the parts of the matched text to include.

A range is entered in the form @nm, where n is the number of the mark at the start of the text to include and m is the mark at the end. Hence the whole of the matched text is @09, for which there is a shorthand version, @@.

Find decimal numbers

Search: {D}+ @1 "." @2 {D}+

Match 1 or more digits followed by a decimal point and then 1 or more digits.

This might be matched by "3.14" in the document. Then:

Replace: @09	is "3.14"
Replace: @01	is "3"
Replace: @02	is "3."
Replace: @12	is "."
Replace: @19	is ".14"

Highlighted range

Marks can also be used in a List of Found search.

The text that is marked bold in the window, is highlighted when a line is clicked or is marked from the menu, is usually the whole of the matched text. This is because it is defined by the range @09.

If you include either of the marks @0 or @9 in the search expression, only the part between them will be highlighted.

Highlight PROC/FN names

ListOfFound: "PROC"|"FN" @0 {AD|"_"}+

Examples

The theory of marks and ranges looks straightforward, but how do you actually use them? Here are a couple of real examples.

Mixed date formats

A document has dates typed in a variety of forms: e.g. "1 1 99", "1.1.99", "(1.1.99)", "1.1.1999", "1/1/99". These need to be standardised as "1/1/99".

Standardise date format

Search:  ["("] @1{D}1:2@2 ' ./' @3{D}1:2@4 ' ./' ["19"] @5 {D}2 @6 [")]"
Replace: @12"/"@34"/19"@56

How this works is left as an exercise for the reader!

Displaced lines

A document consists of sets of lines of text that represent fields within records. Some of the fields are in the wrong order and need to be swapped. In the two below, the conductor's name is either before or after the music. It should be after.

LONDON SYMPHONY ORCHESTRA
Nielsen	Flute Concerto
Mendelssohn	Symphony No 4 'Italian'
Paao Jarvi
Symphony Hall, Birmingham
BBC SYMPHONY ORCHESTRA
Stanislaw Skrowaczewski
Strauss	Also Sprach Zarathustra
Beethoven	Symphony No 7
Royal Festival Hall, London

Note that the orchestra name is in upper case, the conductor's name has no tab but the concert items do, and that the venue always has a comma, which the items do not.

Swap position of lines

Search:  {'A-Z '}+$ @1 {~\t.}+$ @2 {{~",".}+$}+
Replace: @01@29@12

A final point is that the order in which you do replacements can be very important. Plan and test what you are going to do, and keep backups!

Using a mode file

Although StrongED keeps a history of searches during a session, they are lost when you quit and there is no straightforward way to preserve them. You may also find that the length of the icons makes looking at long expressions quite difficult.

The search syntax is used in mode files, which can define named expressions. It would be unwise to modify the BaseMode but it is quite simple to set up a mode specially to aid search and replace.

Type in a new text file, for example:

# search expressions
Search
 spaces	+
 spaces2	" "+
End
# replace expressions
Replace
 space		" "
 tab		"\t"
End

Note that the section contents are indented and that comments start with '#' at the line start.

Save this as "Search" in '!StrED_cfg. Modes', then quit and reload StrongED. Now you can open a document in Search mode, but more probably you will find you want to change to it from BaseMode.

To do this, either follow the main menu 'StrongED>BaseMode>Change mode>Search' or click <menu> on the mode name at the centre of the infobar.

Now you can enter the names in the icons instead of the expressions:

Search:  spaces2
Replace: tab

To modify, or add more, named expressions you must edit and save the mode file. To open the file choose the last item in the main menu, which will be 'Search'.

To make sure the mode file is rescanned when it is saved, you need to tick the 'Reload all modefiles ...' option in the 'Global Choices - Edit'. It might also be useful to tick the 'Show 'previous' text ...' option too.


    Top