Depending upon the pattern:
If you want everything up to the first number. This will grab everything from the start of the string up to, but not including, the first number. This may or may not be what you want because this would also include any other non-numeric characters.
Lua Code:
local word = string.match(SomeString, "^[^0-9]+")
local SomeString= "Word14"
-- using this string it would return Word
local SomeString= "3Word14"
-- using this string it would return nil since the first character is 3
local SomeString= "W.o,r-5d14"
-- using this string it Would return W.o,r-
If you only want to grab the first alpha characters you could do:
Lua Code:
local word = string.match(SomeString, "^[a-zA-Z]+")
local SomeString= "Word14"
-- using this string it would return Word
local SomeString= "3Word14"
-- using this string it would return nil since the first character is not a lower or uppercase letter
local SomeString= "W,o.r-d14"
-- using this string it would return W since the next character is not a lower or uppercase letter
In the following:
Lua Code:
local word = string.match(SomeString, "^[^0-9]+")
-- SomeString is the string your going to search through
-- the second parameter is the pattern your going to try and match
--------- In that second parameter (the pattern):
-- the starting (first) ^ means that the string to be found MUST start from the beginning of the string
-- Stuff inside [] means match any of those characters, so [0-9] would mean match 0,1,2,3,4...,9
-- However when you add the (second) ^ inside of [^..] it means match anything NOT in those brackets
-- So our [^0-9] means match anything that is NOT a number
-- The + means Match the previous element one or more times
-- So in all it says: ^[0-9]+
-- ^ Starting from the beginning of the string
-- [^0-9] Match non-numeric characters
-- + one or more times
In the other example I gave:
Lua Code:
local word = string.match(SomeString, "^[a-zA-Z]+")
-- In this pattern, the [a-zA-Z] means match any lowercase or uppercase letter
-- So this pattern says:
-- ^ Starting at the beginning of the string
-- [a-zA-Z] Match any a lower or uppercase letter
-- + one or more times
Edit:
After my post I see the other responses and may have misunderstood what you wanted. I assumed you only wanted the "Word" and not the 14. If you just wanted to separate but keep both the "Word" and the "14"
You could change it to this:
Lua Code:
local word, word2 = string.match(sString, "^([a-zA-Z]+)([0-9]+)")
The ()'s mean capture & return that part of the pattern.
So the ([a-zA-Z]+) Means capture that part of the pattern and it is the first return since it is the first set of ()'s
The second set: ([0-9]+) Means capture that part of the pattern & return it. It is the 2nd return since its the second set of ()'s
Lua Code:
local sString = "Word14"
-- Using that string would return:
local word, word2 = string.match(sString, "^([a-zA-Z]+)([0-9]+)")
word = Word
word2 = 14
And in their examples, just so you understand, they were doing the same thing.
%a means letters
%d means digits.
Here are a few others:
Lua Code:
. all characters
%a letters
%c control characters
%d digits
%l lower case letters
%p punctuation characters
%s space characters
%u upper case letters
%w alphanumeric characters
%x hexadecimal digits
%z the character with representation 0
In merlights example :
The $ means it must match to the end of the string.
Lua Code:
local word, num = text:match("^(%w+)(%d+)$")
The $ at the end means it must match all the way to the end of the string, so if I had thrown a $ on the end of my pattern:
Lua Code:
local word, num = string.match(sString, "^([a-zA-Z]+)([0-9]+)$")
local sString = "Word14"
-- would return word = Word & num = 14
-- but
local sString = "Wo3rd14"
-- would return nil, nil because the pattern of
-- ^([a-zA-Z]+) starting the string with letters ONLY
-- ([0-9]+)$ followed by numbers ONLY all the way to the end of the string
-- That pattern can not be found because after the 3, there is another letter.