Jump to content

Regular expressions: Difference between revisions

→‎{{header|Lua}}: Add note about Lua patterns vs. regular expressions, show some pattern syntax in example and substitution with match reference
m (→‎{{header|Python}}: PEP-8 whitespace, Python 3 compatibility)
(→‎{{header|Lua}}: Add note about Lua patterns vs. regular expressions, show some pattern syntax in example and substitution with match reference)
Line 891:
 
=={{header|Lua}}==
<lang lua>str1 = "This is a string!"
str2 = "string"
 
In Lua many string manipulation methods use ''patterns'', which offer almost the same fucntionality as regular expressions, but whose syntax differs slightly. The percent sign (<code>%</code>) is generally used instead of a backslash to start a character class or a reference for a match in a substitution.
print( str1:match( str2 ) )
 
erg = str1:gsub( "a", "another" ); print( erg )</lang>
<lang lua>str1test = "ThisMy isname ais string!Lua."
pattern = ".*name is (%a*).*"
 
if test:match(pattern) then
print("Name found.")
end
 
sub, num_matches = test:gsub(pattern, "Hello, %1!")
print(sub)</lang>
 
=={{header|M4}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.