Searching and replacing

gtkscintilla.Scintilla.find_text(text[, range = None[, **flags]])

This method is used to ... yes, find the text in the document.

Parameter:range (length 2 tuple of integers) – if not specified, searches in all the text. To search in a range you have to pass range = (start,end) postions. You can set also reversed range (54,0) - only if regexp is set to False.

flags are the following keyword arguments, default values are set to False. (To activate them you must set them True)

  • matchcase – A match only occurs with text that matches the case of the search string.
  • wholeword – A match only occurs if the characters before and after are not word characters.
  • wordstart – A match only occurs if the character before is not a word character.
  • regexp – The search string should be interpreted as a regular expression.
  • posix – Treat regular expression in a more POSIX compatible manner by interpreting bare ( and ) for tagged sections rather than \( and \).

In a regular expression, special characters interpreted are:

  • . Matches any character
  • \( This marks the start of a region for tagging a match.
  • \) This marks the end of a tagged region.
  • \n Where n is 1 through 9 refers to the first through ninth tagged region when replacing. For example, if the search string was Fred\([1-9]\)XXX and the replace string was Sam\1YYY, when applied to Fred2XXX this would generate Sam2YYY.
  • \< This matches the start of a word using Scintilla’s definitions of words.
  • \> This matches the end of a word using Scintilla’s definition of words.
  • \x This allows you to use a character x that would otherwise have a special meaning. For example, \[ would be interpreted as [ and not as the start of a character set.
  • [...] This 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.
  • ^ This matches the start of a line (unless used inside a set, see above).
  • $ This matches the end of a line.
  • * This matches 0 or more times. For example, Sa*m matches Sm, Sam, Saam, Saaam and so on.
  • + This matches 1 or more times. For example, Sa+m matches Sam, Saam, Saaam and so on.
gtkscintilla.Scintilla.replace_text(text, repl[, range = None[, **flags]])

Replace the first occurence of text with repl. The range and flags arguments are similar to find_text().

Returns:True if the replacement was ok, False if the text wasn’t found.

Searching and replacing using targets

You can also search and replace using the raw Scintilla functions, to use them you have to:
  1. set the target with gtkscintilla.Scintilla.set_target_start() and gtkscintilla.Scintilla.set_target_end() .
  2. In the target you can search using gtkscintilla.Scintilla.search_in_target()
  3. This sets the target to the matching string that you have found so you can replace it using gtkscintilla.Scintilla.replace_target()
These functions set and return the start and end of the target. When searching in non-regular expression mode, you can set start greater than end to find the last matching text in the target rather than the first matching text. The target is also set by a successful gtkscintilla.Scintilla.search_in_target().
Set the target start and end to the start and end positions of the selection.
These get and set the flags used by gtkscintilla.Scintilla.search_in_target(). There are several option flags including a simple regular expression search.
This searches for the first occurrence of a text string in the target defined by gtkscintilla.Scintilla.set_target_start() and gtkscintilla.Scintilla.set_target_end(). The text string is not zero terminated; the size is set by length. The search is modified by the search flags set by gtkscintilla.Scintilla.set_search_flags(). If the search succeeds, the target is set to the found text and the return value is the position of the start of the matching text. If the search fails, the result is -1.
After replacement, the target range refers to the replacement text. The return value is the length of the replacement string. Note that the recommended way to delete text in the document is to set the target to the text to be removed, and to perform a replace target with an empty string.
This replaces the target using regular expressions. The replacement string is formed from the text string with any sequences of 1 through 9 replaced by tagged matches from the most recent regular expression search. After replacement, the target range refers to the replacement text. The return value is the length of the replacement string.

Table Of Contents

Previous topic

Tabs and Indentation Guides

Next topic


This Page