Difference between revisions of "String.gmatch"

From GiderosMobile
Line 2: Line 2:
 
'''Available since:''' Gideros 2011.6<br/>
 
'''Available since:''' Gideros 2011.6<br/>
 
=== Description ===
 
=== Description ===
Returns an iterator function that, each time it is called, returns the next captures from pattern over string s. If pattern specifies no captures, then the whole match is produced in each call.  
+
<translate>Returns an iterator function that, each time it is called, returns the next captures from pattern over string s. If pattern specifies no captures, then the whole match is produced in each call.  
  
  
 
  As an example, the following loop  
 
  As an example, the following loop  
  
     `s = "hello world from Lua"`
+
     [[[s = "hello world from Lua"]]]
     `for w in string.gmatch(s, "%a+") do`
+
     [[[for w in string.gmatch(s, "%a+") do]]]
       `print(w)`
+
       [[[print(w)]]]
     `end`
+
     [[[end]]]
 
</pre>
 
</pre>
  
 
  will iterate over all the words from string s, printing one per line. The next example collects all pairs key=value from the given string into a table:  
 
  will iterate over all the words from string s, printing one per line. The next example collects all pairs key=value from the given string into a table:  
  
     `t = {}`
+
     [[[t = {}]]]
     `s = "from=world, to=Lua"`
+
     [[[s = "from=world, to=Lua"]]]
     `for k, v in string.gmatch(s, "(%w+)=(%w+)") do`
+
     [[[for k, v in string.gmatch(s, "(%w+)=(%w+)") do]]]
       `t[k] = v`
+
       [[[t[k] = v]]]
     `end`
+
     [[[end]]]
  
  
  For this function, a '^' at the start of a pattern does not work as an anchor, as this would prevent the iteration.
+
  For this function, a '^' at the start of a pattern does not work as an anchor, as this would prevent the iteration.</translate>
 
<source lang="lua">
 
<source lang="lua">
 
(function) = string.gmatch(s,pat)
 
(function) = string.gmatch(s,pat)
 
</source>
 
</source>
 
=== Parameters ===
 
=== Parameters ===
'''s''': (string) string where to look for patterns <br/>
+
'''s''': (string) <translate>string where to look for patterns</translate> <br/>
'''pat''': (string) pattern to look for <br/>
+
'''pat''': (string) <translate>pattern to look for</translate> <br/>
 
=== Return values ===
 
=== Return values ===
'''Returns''' (function) iterator function<br/>
+
'''Returns''' (function) <translate>iterator function</translate><br/>

Revision as of 13:33, 23 August 2018

Available since: Gideros 2011.6

Description

Returns an iterator function that, each time it is called, returns the next captures from pattern over string s. If pattern specifies no captures, then the whole match is produced in each call.


As an example, the following loop 
    [[[s = "hello world from Lua"]]]
    [[[for w in string.gmatch(s, "%a+") do]]]
      [[[print(w)]]]
    [[[end]]]
will iterate over all the words from string s, printing one per line. The next example collects all pairs key=value from the given string into a table: 
    [[[t = {}]]]
    [[[s = "from=world, to=Lua"]]]
    [[[for k, v in string.gmatch(s, "(%w+)=(%w+)") do]]]
      [[[t[k] = v]]]
    [[[end]]]


For this function, a '^' at the start of a pattern does not work as an anchor, as this would prevent the iteration.
(function) = string.gmatch(s,pat)

Parameters

s: (string) string where to look for patterns
pat: (string) pattern to look for

Return values

Returns (function) iterator function