Thread Tools Display Modes
10/18/14, 12:43 PM   #1
Randactyl
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 251
Quick String Question

Say I have a string of the pattern "Word14" and want to separate the word from the number. How would I go about that?

I assume gsub would be used? I haven't been able to find an example that my brain will allow me to comprehend at the moment
  Reply With Quote
10/18/14, 12:59 PM   #2
merlight
AddOn Author - Click to view addons
Join Date: Jul 2014
Posts: 671
Lua Code:
  1. local word, num = text:match("^(%a+)(%d+)$")
  2. num = tonumber(num)

Last edited by merlight : 10/18/14 at 03:00 PM.
  Reply With Quote
10/18/14, 01:09 PM   #3
Garkin
 
Garkin's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 832
Another option is using string.find

With captures:
Lua Code:
  1. local text = "Word14"
  2. local _, _, word, number = text:find("(%a+)(%d+)")
  3. number = tonumber(number)

Without captures:
Lua Code:
  1. word = text:sub(text:find("^%a+")) --finds all letters at the beggining of the string
  2. number = text:sub(text:find("%d+$")) --finds all digits at the end of the string
  3. number = tonumber(number)


Using the string.gsub:
Lua Code:
  1. word = text:gsub("%d", "") --removes all digits from the string
  2. number = text:gsub("%a", "") --removes all letters from the string
  3. number = tonumber(number)

Last edited by Garkin : 10/18/14 at 01:27 PM.
  Reply With Quote
10/18/14, 01:13 PM   #4
circonian
AddOn Author - Click to view addons
Join Date: May 2014
Posts: 613
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:
  1. local word =  string.match(SomeString, "^[^0-9]+")
  2.     local SomeString= "Word14"
  3. -- using this string it would return Word
  4.  
  5.     local SomeString= "3Word14"
  6. -- using this string it would return nil since the first character is 3
  7.  
  8.     local SomeString= "W.o,r-5d14"
  9. -- using this string it Would return W.o,r-

If you only want to grab the first alpha characters you could do:
Lua Code:
  1. local word =  string.match(SomeString, "^[a-zA-Z]+")
  2.     local SomeString= "Word14"
  3. -- using this string it would return Word
  4.  
  5.     local SomeString= "3Word14"
  6. -- using this string it would return nil since the first character is not a lower or uppercase letter
  7.  
  8.     local SomeString= "W,o.r-d14"
  9. -- using this string it would return W since the next character is not a lower or uppercase letter


In the following:
Lua Code:
  1. local word =  string.match(SomeString, "^[^0-9]+")
  2. -- SomeString is the string your going to search through
  3. -- the second parameter is the pattern your going to try and match
  4.  
  5. --------- In that second parameter (the pattern):
  6. -- the starting (first) ^ means that the string to be found MUST start from the beginning of the string
  7. -- Stuff inside []  means match any of those characters, so [0-9] would mean match 0,1,2,3,4...,9
  8. -- However when you add the  (second) ^ inside of [^..] it means match anything NOT in those brackets
  9. -- So our [^0-9] means match anything that is NOT a number
  10. -- The + means Match the previous element one or more times
  11.  
  12. -- So in all it says: ^[0-9]+
  13. -- ^ Starting from the beginning of the string
  14. -- [^0-9] Match non-numeric characters
  15. -- + one or more times

In the other example I gave:
Lua Code:
  1. local word =  string.match(SomeString, "^[a-zA-Z]+")
  2. -- In this pattern, the [a-zA-Z] means match any lowercase or uppercase letter
  3. -- So this pattern says:
  4. -- ^ Starting at the beginning of the string
  5. -- [a-zA-Z] Match any a lower or uppercase letter
  6. -- +   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:
  1. 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:
  1. local sString = "Word14"
  2. -- Using that string would return:
  3. local word, word2 =  string.match(sString, "^([a-zA-Z]+)([0-9]+)")
  4. word = Word
  5. 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:
  1. .   all characters
  2. %a  letters
  3. %c  control characters
  4. %d  digits
  5. %l  lower case letters
  6. %p  punctuation characters
  7. %s  space characters
  8. %u  upper case letters
  9. %w  alphanumeric characters
  10. %x  hexadecimal digits
  11. %z  the character with representation 0

In merlights example :
The $ means it must match to the end of the string.

Lua Code:
  1. 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:
  1. local word, num =  string.match(sString, "^([a-zA-Z]+)([0-9]+)$")
  2.     local sString = "Word14"
  3. -- would return word = Word   &   num = 14
  4. -- but
  5.     local sString = "Wo3rd14"
  6. -- would return nil, nil because the pattern of
  7. -- ^([a-zA-Z]+)  starting the string with letters ONLY
  8. -- ([0-9]+)$  followed by numbers ONLY all the way to the end of the string
  9. -- That pattern can not be found because after the 3, there is another letter.

Last edited by circonian : 10/18/14 at 01:44 PM.
  Reply With Quote
10/18/14, 03:00 PM   #5
merlight
AddOn Author - Click to view addons
Join Date: Jul 2014
Posts: 671
Oh sorry, my pattern was wrong. %w would of course eat the number, too. I'll edit that so it doesn't confuse you
  Reply With Quote
10/18/14, 03:44 PM   #6
Randactyl
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 251
Awesome. Thanks for all the examples guys
  Reply With Quote

ESOUI » Developer Discussions » Lua/XML Help » Quick String Question


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off