Counting operators in a string

1.) Is there a way to count how many operators there are in a given string? I’m only dealing with “+” if that matters.

2.) Also, just as important, how can I clean out all unwanted character(s)? Does the following look possible?

str = string.gsub(str, “%s+”, “”) – all space characters

str = string.gsub(str, “%a+”, “”) – all letters

str = string.gsub(str, “%p+”, “”) – all punctuation

Is there a way to combine these? For instance:

     str = string.gsub(str, “%s+” or “%a+” or “%p+” , “”)

Or is there an easier way to go about cleaning out my string since all I’m concerned about are digits and the “+” operator by using the “.” character class to represent all characters (and then specify out the digits and operator).

Thanks very much in advance!

  1. this is the way i do it (but there might be another “shorter” way as well, nevertheless it works.

counting operators in a string:
[lua]

local plusCnt = 0
local s = “hello + world + this + is + a + test”

for w in string.gmatch(s, “+”) do
    plusCnt = plusCnt+1
end
print(plusCnt) --5
[/lua]
 


 
Is there a way to combine these? For instance:
     str = string.gsub(str, “%s+” or “%a+” or “%p+” , “”)

 
 
yip.
[lua]
local s = “1 hello + 2 world + 3 this + 4 is + a + 5 test”
s = string.gsub(s, “[%s+%a+%p+]”, “”)
print(s) --12345
[/lua] 

 

via http://www.lua.org/manual/5.2/manual.html#6.4

  • [set]:
    represents the class which is the union of all
    characters in set.
    A range of characters can be specified by
    separating the end characters of the range,
    in ascending order, with a ‘-’,
    All classes %x described above can also be used as
    components in set.
    All other characters in set represent themselves.
    For example, [%w_] (or [_%w])
    represents all alphanumeric characters plus the underscore,
    [0-7] represents the octal digits,
    and [0-7%l%-] represents the octal digits plus
    the lowercase letters plus the ‘-’ character.

The interaction between ranges and classes is not defined.
Therefore, patterns like [%a-z] or [a-%%]
have no meaning.

  1. this is the way i do it (but there might be another “shorter” way as well, nevertheless it works.

counting operators in a string:
[lua]

local plusCnt = 0
local s = “hello + world + this + is + a + test”

for w in string.gmatch(s, “+”) do
    plusCnt = plusCnt+1
end
print(plusCnt) --5
[/lua]
 


 
Is there a way to combine these? For instance:
     str = string.gsub(str, “%s+” or “%a+” or “%p+” , “”)

 
 
yip.
[lua]
local s = “1 hello + 2 world + 3 this + 4 is + a + 5 test”
s = string.gsub(s, “[%s+%a+%p+]”, “”)
print(s) --12345
[/lua] 

 

via http://www.lua.org/manual/5.2/manual.html#6.4

  • [set]:
    represents the class which is the union of all
    characters in set.
    A range of characters can be specified by
    separating the end characters of the range,
    in ascending order, with a ‘-’,
    All classes %x described above can also be used as
    components in set.
    All other characters in set represent themselves.
    For example, [%w_] (or [_%w])
    represents all alphanumeric characters plus the underscore,
    [0-7] represents the octal digits,
    and [0-7%l%-] represents the octal digits plus
    the lowercase letters plus the ‘-’ character.

The interaction between ranges and classes is not defined.
Therefore, patterns like [%a-z] or [a-%%]
have no meaning.

Thank you very much for the good info! I just started working with this and so far it’s doing well on deleting everything I’m not interested in (spaces, etc). I was surprised though that %p+ included the magic “+” character though. I thought it was usually excluded? Is there a way to somehow include %p+ but tell it to ignore the “+” symbol? Otherwise my other option I learned was to individually exclude all punctuation I wasn’t interested using %x from this character class list: http://docs.coronalabs.com/guide/data/luaString/index.html

So for example %(+%)+%,+… and so on. 

Thanks for all the big help!

Thank you very much for the good info! I just started working with this and so far it’s doing well on deleting everything I’m not interested in (spaces, etc). I was surprised though that %p+ included the magic “+” character though. I thought it was usually excluded? Is there a way to somehow include %p+ but tell it to ignore the “+” symbol? Otherwise my other option I learned was to individually exclude all punctuation I wasn’t interested using %x from this character class list: http://docs.coronalabs.com/guide/data/luaString/index.html

So for example %(+%)+%,+… and so on. 

Thanks for all the big help!