Difference between revisions of "String.gmatch"

From GiderosMobile
(formatting)
Line 1: Line 1:
 
__NOTOC__
 
__NOTOC__
 +
'''<translate>Available since</translate>:''' Gideros 2011.6<br/>
 +
'''<translate>Class</translate>:''' [[Special:MyLanguage/string|string]]<br/>
  
<languages />
+
=== Description ===
 
 
'''<translate>Available since</translate>:''' Gideros 2011.6
 
<br/>
 
 
 
'''<translate>Class</translate>:''' [[Special:MyLanguage/string|string]]
 
<br/>
 
 
 
=== <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.
 
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>
+
<source lang="lua">
 +
(function) = string.gmatch(s,pat)
 +
</source>
  
<translate>
+
=== Parameters ===
As an example, the following loop
+
'''s''': (string) <translate>string where to look for patterns</translate><br/>
</translate>
+
'''pat''': (string) <translate>pattern to look for</translate><br/>
  
<pre>
+
=== Return values ===
  ''s = "hello world from Lua"''
+
'''<translate>Returns</translate>''' (function) <translate>iterator function</translate><br/>
  ''for w in string.gmatch(s, "%a+") do''
 
    ''print(w)''
 
  ''end''
 
</pre>
 
  
<translate>
+
=== Examples ===
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:  
+
As an example, the following loop will iterate over all the words from string s, printing one per line:
</translate>
+
<source lang="lua">
 
+
s = "hello world from Lua"
<pre>
+
for w in string.gmatch(s, "%a+") do
  ''t = {}''
+
  print(w)
  ''s = "from=world, to=Lua"''
+
end
  ''for k, v in string.gmatch(s, "(%w+)=(%w+)") do''
+
</source>
    ''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>
 
  
 +
The next example collects all pairs key=value from the given string into a table:
 
<source lang="lua">
 
<source lang="lua">
(function) = string.gmatch(s,pat)
+
t = {}
 +
s = "from=world, to=Lua"
 +
for k, v in string.gmatch(s, "(%w+)=(%w+)") do
 +
  t[k] = v
 +
end
 
</source>
 
</source>
 
+
For this function, a '^' at the start of a pattern does not work as an anchor, as this would prevent the iteration.
=== <translate>Parameters</translate> ===
 
'''s''': (string) <translate>string where to look for patterns</translate>
 
<br/>
 
'''pat''': (string) <translate>pattern to look for</translate>
 
<br/>
 
 
 
=== <translate>Return values</translate> ===
 
'''<translate>Returns</translate>''' (function) <translate>iterator function</translate><br/>
 
  
 
{{String}}
 
{{String}}

Revision as of 03:24, 24 August 2020

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.

(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

Examples

As an example, the following loop will iterate over all the words from string s, printing one per line:

s = "hello world from Lua"
for w in string.gmatch(s, "%a+") do
   print(w)
end

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.