Ref, your .love is good, but I have some corrections (no biggies, though. Just little things):
Line 47: %w gets
alphanumeric characters, not only upper case letters.
Lines 58 & 60: Correct, but needs emphasis that these can be anything, not just letters.
Line 71: Returns the last capture of that appears 0 or more times.
Line 82: Returns the first capture.
Like I said, no huge problems. They're probably my fault anyway, for not being clear enough.
String captures do have a lot of applicable uses, especially if you're handling user input. Here are some examples:
Code: Select all
function CapitalizeFirstLetter( String, ConditionFunction )
ConditionFunction = ConditionFunction or function( String ) return String end
local function Function( First, Rest )
if ConditionFunction( First .. Rest ) then
return First:upper() .. Rest:lower()
end
end
return String:gsub( '(%a)([%w_\']*)', Function )
end
CapitalizeFirstLetter( 'this is a test text' ) --> This Is A Test Text
CapitalizeFirstLetter( 'this is a test text', function( String ) return #String > 3 and String end ) --> This is a Test Text
Now let's look at what's happening in the string-portion of this function:
Code: Select all
return String:gsub( '(%a)([%w_\']*)', Function )
So,
Sting:gsub( ... ) is the same as
string.gsub( String, ... ). Putting parenthesis around the capture makes it an argument for the function (First, Rest). So, what happens is it captures the first letter, then captures the first word, non-alphanumeric, underscore, or " ' " character, and gets all of those. It then passes those to the function. The function makes the first upper-case, and the rest lower case.
Here's another. This one removes white-spaces around the front and back of a text. Don't worry, it's a lot shorter.
Code: Select all
function RemoveEndWhiteSpace( String )
return String:match( '^[%s]*(.-)[%s]*$' )
end
So here's what this does:
^[%s]* -is just fancy for: return all the spaces at the beginning (^ at the beginning, but not inside the brackets) of the string.
[%s]* -$ means the same, but for the end of the string (as indicated by the $ at the end).
(.-) -This returns the first words that come in-between the spaces at the beginning and end of the word. The - is necessary because if you didn't use it, you would get the spaces at the end as well. That wasn't too bad, was it?
Code: Select all
function RemoveExtraWhiteSpace( String, ConditionFunction )
ConditionFunction = ConditionFunction or function( String ) return String end
local function Function( First, Last )
if ConditionFunction( First .. Last ) then
if #Last > 0 then
return ' '
end
return First .. Last
end
end
String = String:gsub( '(%s)(%s+%w-)', Function )
return RemoveEndWhiteSpace( String )
end
Here's the string part:
Code: Select all
String = String:gsub( '(%s)(%s+%w-)', Function )
This captures the first space as the first argument, and then a space and the first alphanumeric character.
There are more, but those are some basic examples.
TL;DR- Just read it. You may learn something.