![]() ("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). Live Demo function mysplit (inputstr, sep)įor str in string.First let's take a look at the string.find function in general: The above example works fine for simple patterns but when we want to make use of a regular expression and the case that we can have a default pattern with which to split, we can consider a more elegant example. The string.sub () function takes three arguments in general, the first argument being the name of the string from which we want to extract a piece, the second argument is the i-th index or say, the starting index of the string piece that we want, and the third and the last argument is the j-th index of the last index of the string piece we want. Exampleįor i in string.gmatch(example, "%S+") do In Lua, there is no split function that is present inside the standard library but we can make use of other functions to do the work that normally a split function would do.Ī very simple example of a split function in Lua is to make use of the gmatch() function and then pass a pattern which we want to separate the string upon. Splitting a string is the process in which we pass a regular expression or pattern with which one can split a given string into different parts. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
December 2022
Categories |