![]() ("helelo"):find "h. l" - will match as much as it gets ("helllllo"):find "el*" -> will match elllll ("heo"):find "el " -> won't match anything ("helllllo"):find "el " -> will match elllll ("hello"):find "o%d?" -> matches o, returns 5, 5 will match all EXCEPT the letters s, t, a, c and k and the space character ("stack overflow"):find "" - will match an 'a', a 'b' or a 'c' ("137'5 m47ch s0m3 d1g175"):find "m%d%d" - will match an m followed by 2 digit Any of these characters can be represented by a % following the character itself. Introducing Patterns ("hello world"):find ".- " - will match characters until it finds a spaceĪll except the following characters represent themselves ^$()%.* -?). equivalent to string.find("Hello, I am a string", "am") - see remarks ("Hello, I am a string"):find "am" -> returns 10 11 The function string.find (s, substr ]) returns the start and end index of a substring if found, and nil otherwise, starting at the index init if it is provided (defaults to 1). String.find (Introduction) The find functionįirst let's take a look at the string.find function in general: Matches substring between two distinct characters (balanced pair of x and y)įrontier pattern: matches an empty string at any position such that the next characterīelongs to set and the previous character does not belong to set Lua pattern matching engine provides a few additional pattern matching items: Character itemįor n between 1 and 9 matches a substring equal to the n-th captured string The modifier ? matches an optional character, allowing you to match, for example, a negative digit: local foo = "-20" The character - is also similar to *, but instead of returning the longest matched sequence, it matches the shortest one. The character represents one or more matched characters in the sequence and it will always return the longest matched sequence: local foo = "12345678bar123"Īs you can see, * is similar to , but it accepts zero occurrences of characters and is commonly used to match optional spaces between different patterns. Patterns can be more useful with the help of repetition/optional modifiers, patterns in lua offer these four characters: Character In this example, string.match will find the first occurrence that isn't b, a or r. You can get the complement of the character set by starting it with ^: local foo = "bar123bar2341" You can use the % character with any other non-alphanumeric character, therefore, if you need to escape, for instance, a quote, you must use \\ before it, which escapes any character from a lua string.Ī character set, represented inside square brackets ( ), allows you to create a special character class, combining different classes and single characters: local foo = "bar123bar2341" The character % represents a character escape, making %? match an interrogation and %% match the percentage symbol. In addition to character classes, some characters have special functions as patterns: ( ) %. For instance, %D will match any non-digit character sequence: string.match("f123", "%D") -> f Character classĪs mentioned above, any upper-case version of those classes represents the complement of the class. For instance, the character sequence %a matches any letter, while its upper-case version represents all non-letters characters, all characters classes (a character sequence that, as a pattern, can match a set of items) are listed below. Both can be very similar, but Lua pattern matching is more limited and has a different syntax. Instead of using regex, the Lua string library has a special set of characters used in syntax matches. Throughout some examples, the notation ():function is used, which is equivalent to string.function(, ) because all strings have a metatable with the _index field set to the string table. ? match exactly 0 or 1 occurrence of previous character class ![]() lazy match 0 or more occurrences of previous character class greedy match 1 or more occurrences of previous character class * greedy match 0 or more occurrences of previous character class represents the class which is the union of all characters in set. %g represents all printable characters except space String.gsub(str, pattern, repl ) - Replaces substrings (up to a max of n times) String.gmatch(str, pattern) - Returns a function that iterates through all matches in str String.match(str, pattern ) - Matches a pattern once (starting at index) String.find(str, pattern ]) - Returns start and end index of match in str
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |