Difference between revisions of "String.gmatch"

From GiderosMobile
m (→‎Description: fixed some html tags)
Line 1: Line 1:
 
__NOTOC__
 
__NOTOC__
 +
 
<languages />
 
<languages />
'''<translate>Available since</translate>:''' Gideros 2011.6<br/>
+
 
'''<translate>Class</translate>:''' [[Special:MyLanguage/string|string]]<br/>
+
'''<translate>Available since</translate>:''' Gideros 2011.6
 +
<br/>
 +
 
 +
'''<translate>Class</translate>:''' [[Special:MyLanguage/string|string]]
 +
<br/>
 +
 
 
=== <translate>Description</translate> ===
 
=== <translate>Description</translate> ===
<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.  
+
<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.
 +
</translate>
  
As an example, the following loop  
+
<translate>
 +
As an example, the following loop  
 +
</translate>
  
    ''s = "hello world from Lua"''
+
<pre>
    ''for w in string.gmatch(s, "%a+") do''
+
  ''s = "hello world from Lua"''
      ''print(w)''
+
  ''for w in string.gmatch(s, "%a+") do''
    ''end''
+
    ''print(w)''
 +
  ''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:  
+
<translate>
 +
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:  
 +
</translate>
  
    ''t = {}''
+
<pre>
    ''s = "from=world, to=Lua"''
+
  ''t = {}''
    ''for k, v in string.gmatch(s, "(%w+)=(%w+)") do''
+
  ''s = "from=world, to=Lua"''
      ''t[k] = v''
+
  ''for k, v in string.gmatch(s, "(%w+)=(%w+)") do''
    ''end''
+
    ''t[k] = v''
 +
  ''end''
 +
</pre>
  
 +
<translate>
 +
For this function, a '^' at the start of a pattern does not work as an anchor, as this would prevent the iteration.
 +
</translate>
  
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>
 +
 
=== <translate>Parameters</translate> ===
 
=== <translate>Parameters</translate> ===
'''s''': (string) <translate>string where to look for patterns</translate> <br/>
+
'''s''': (string) <translate>string where to look for patterns</translate>
'''pat''': (string) <translate>pattern to look for</translate> <br/>
+
<br/>
 +
'''pat''': (string) <translate>pattern to look for</translate>
 +
<br/>
 +
 
 
=== <translate>Return values</translate> ===
 
=== <translate>Return values</translate> ===
'''<translate>Returns</translate>''' (function) <translate>iterator function</translate><br/>
+
'''<translate>Returns</translate>''' (function) <translate>iterator function</translate>
 +
<br/>
 +
<br/>

Revision as of 21:57, 17 June 2019



Available since: Gideros 2011.6

Class: string

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