Difference between revisions of "String.gmatch"

From GiderosMobile
 
(7 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
__NOTOC__
 
__NOTOC__
<languages />
+
'''Available since:''' Gideros 2011.6<br/>
'''<translate>Available since</translate>:''' Gideros 2011.6<br/>
+
'''Class:''' [[string]]<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.
 
  
 +
=== Description ===
 +
Returns an iterator function that, each time it is called, returns the next captures from pattern ''pat'' over string ''s''. If pattern specifies no captures, then the whole match is produced in each call.
 +
<syntaxhighlight lang="lua">
 +
(function) = string.gmatch(s,pat)
 +
</syntaxhighlight>
  
  As an example, the following loop
+
  '''note: the ''gfind'' method was deprecated in Lua 5.1, and in Lua 5.2, it was replaced by ''gmatch'''''
  
    ''s = "hello world from Lua"''
+
=== Parameters ===
    ''for w in string.gmatch(s, "%a+") do''
+
'''s''': (string) string where to look for patterns<br/>
      ''print(w)''
+
'''pat''': (string) pattern to look for<br/>
    ''end''
 
</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:
+
=== Return values ===
 +
'''Returns''' (function) iterator function<br/>
  
    ''t = {}''
+
=== Examples ===
    ''s = "from=world, to=Lua"''
+
'''The following loop will iterate over all the words from string s, printing one per line'''
    ''for k, v in string.gmatch(s, "(%w+)=(%w+)") do''
+
<syntaxhighlight lang="lua">
      ''t[k] = v''
+
s = "hello world from Lua"
    ''end''
+
for w in string.gmatch(s, "%a+") do
 +
  print(w)
 +
end
 +
-- if you want to print each letter individually rather than sequences just remove the '+' sign
 +
</syntaxhighlight>
  
 +
'''The next example collects all pairs key=value from the given string into a table'''
 +
<syntaxhighlight lang="lua">
 +
t = {}
 +
s = "from=world, to=Lua"
 +
for k, v in string.gmatch(s, "(%w+)=(%w+)") do
 +
  t[k] = v
 +
end
 +
</syntaxhighlight>
 +
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>
+
{{String}}
<source lang="lua">
 
(function) = string.gmatch(s,pat)
 
</source>
 
=== <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/>
 

Latest revision as of 03:45, 17 November 2023

Available since: Gideros 2011.6
Class: string

Description

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

(function) = string.gmatch(s,pat)
note: the gfind method was deprecated in Lua 5.1, and in Lua 5.2, it was replaced by gmatch

Parameters

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

Return values

Returns (function) iterator function

Examples

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
-- if you want to print each letter individually rather than sequences just remove the '+' sign

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.