You can look ahead in the string without actually scanning. There are other methods besides the plain scanners. This is demonstrated by the example above. In both cases, the scan pointer moves just beyond the last character of the match, ready to scan again from the next character onwards. If you use scan_until, then the match can occur anywhere after the scan pointer. When you scan for a pattern (a regular expression), the match must occur at the character after the scan pointer. Given the string “test string”, here are the pertinent scan pointer positions: t e s t s t r i n g The point of scanning is to move forward a bit at a time, so matches are sought after the scan pointer usually immediately after it. Scanning a string means remembering the position of a scan pointer, which is just an index. Here is an example of its usage: s = StringScanner. Special match variables such as $1, $2, $`, $&, and $' are set appropriately.StringScanner provides for lexical scanning operations on a String. If argument replacement is a hash, and pattern matches one of its keys, the replacing string is the value for that key: h = # => "ABCD" You may want to use the block form to avoid a lot of backslashes. If you want to write a non-back-reference string \& in replacement, you need first to escape the backslash to prevent this method from interpreting it as a back-reference, and then you need to escape the backslashes again to prevent a string literal from consuming them: ".\\\\&.". For example, if you want to write a back-reference \& in replacement with a double-quoted string literal, you need to write ".\\&.". See String Literals for details about string literals.Ī back-reference is typically preceded by an additional backslash.
![ruby docs stringscan ruby docs stringscan](http://teendamer.weebly.com/uploads/1/3/3/1/133104943/866330099_orig.jpg)
Note also that a string literal consumes backslashes. Note that \\ is interpreted as an escape, i.e., a single backslash. + corresponds to $+, which contains last capture group. \` corresponds to $`, which contains string before match. \' corresponds to $', which contains string after match. \& and \0 correspond to $&, which contains the complete matched text. However, you may refer to some special match variables using these combinations: Note that within the string replacement, a character combination such as $& is treated as ordinary text, and not as a special match variable. \n ( n a non-negative integer) refers to $n. String replacement may contain back-references to the pattern's captures: If replacement is a string, that string will determine the replacing string that is to be substituted for the matched text.Įach of the examples above uses a simple string as the replacing string.
![ruby docs stringscan ruby docs stringscan](https://raw.githubusercontent.com/thedevslot/WhatATheme/master/assets/images/What%20is%20Jekyll%20and%20How%20to%20use%20it.png)
When pattern is a string, all its characters are treated as ordinary characters (not as regexp special characters): 'THX1138'. The examples in this section mostly use methods String#sub and String#gsub the principles illustrated apply to all four substitution methods.Īrgument pattern is commonly a regular expression: s = 'hello' s. String#gsub!: Zero or more substitutions returns self.Ī first argument, pattern (string or regexp), that specifies the substring(s) to be replaced.Ī second argument, replacement (string or hash), that determines the replacing string.Ī block that will determine the replacing string.
![ruby docs stringscan ruby docs stringscan](https://raw.githubusercontent.com/crookedneighbor/ruby-node/master/docs/assets/images/logos.png)
String#gsub: Zero or more substitutions returns a new string. String#sub!: One substitution (or none) returns self. String#sub: One substitution (or none) returns a new string. However, a method without a bang can also mutate, such as String#replace. In general, if there exist both bang and non-bang version of method, the bang! mutates and the non-bang! does not. Typically, a method whose name ends with ! modifies self and returns self often a similarly named method (without the !) returns a new string. You can convert certain objects to Strings with: You can create a String object explicitly with: String objects differ from Symbol objects in that Symbol objects are designed to be used as identifiers, instead of text or data.
![ruby docs stringscan ruby docs stringscan](https://staticdelivery.nexusmods.com/mods/3333/images/thumbnails/4920/4920-1659037701-732950018.png)
A String object may be created using String::new or as literals. A String object has an arbitrary sequence of bytes, typically representing text or binary data.