I checking string for a non-alphanumeric char.
if(str:match("%W")) then
--make str alpha-numeric
end
How to remove all non-alphanumeric chars from string using lua?
Use gsub (as suggested by Egor Skriptunoff):
str = str:gsub('%W','')
just make it like this
you forgot +
if(str:match("%W+")) then --if it contain alpha
number = str:match("%d+")
alpha = str:match("%W+")
end
Related
I have a word-filter that searches for words using this regex:
/\b[a-zA-Z-]+\b/
Searching the word: "hello".
Case 1: "Hello there" = true
Case 2: "Hello0 there" = false
Case 3: "Hello_there" = false
Case 4: "Hello-there" = false
Case 5: "4Hello there" = false
How can I setup the Word Boundaries to also find words that start/end with a number, underscore, hyphen or any other character other than a letter?
You may use
/(?<![^\W\d])[a-zA-Z]+(?![^\W\d])/
The (?<![^\W\d]) negative lookbehind matches a location that is not immediately preceded with a char other than a non-word and a digit char, i.e. there must be either start of string or a word char but a digit.
The (?![^\W\d]) negative lookahead matches a location that is not immediately followed with a char other than a non-word and a digit char, i.e. there must be either end of string or a word char other than a digit.
What I'm looking for is something like the following, but it only applies to the first find it gets.
str:gsub("1", "")
I'd like it to only delete the first 1 it finds OR just the first word of the string.
How would I go about doing this?
try this:
local str = "234243 232564 se42"
local str, i = str:gsub("1", "",1)
print (str,i)
str = (i>0) and str or str:gsub("^.-%s", "",1)
print (str)
only when there are spaces in the string (more than one word).
I need a special Lua pattern that takes all the uppercase letters in a string, and replaces them with a space and the respective lowercase letter;
TestStringOne => test string one
this isA TestString => this is a test string
Can it be done?
Assuming only ASCII is used, this works:
function lowercase(str)
return (str:gsub("%u", function(c) return ' ' .. c:lower() end))
end
print(lowercase("TestStringOne"))
print(lowercase("this isA TestString"))
function my(s)
s = s:gsub('(%S)(%u)', '%1 %2'):lower()
return s
end
print(my('TestStringOne')) -->test string one
print(my('this isA TestString')) -->this is a test string
I'm trying to split a string into 2 strings when main string is over 30 chars and separator I wanted to use is a simple space between chars(the last space between words in main string) so it won't cut words. I'm asking you guys for help because I'm not very good with patterns in Lua.
local function split(str, max_line_length)
local lines = {}
local line
str:gsub('(%s*)(%S+)',
function(spc, word)
if not line or #line + #spc + #word > max_line_length then
table.insert(lines, line)
line = word
else
line = line..spc..word
end
end
)
table.insert(lines, line)
return lines
end
local main_string = 'This is very very very very very very long string'
for _, line in ipairs(split(main_string, 20)) do
print(line)
end
-- Output
This is very very
very very very very
long string
If you just want to split the string at the last space between words, try this
s="How to split string by string length and a separator"
a,b=s:match("(.+) (.+)")
print(s)
print(a)
print(b)
How do I capitalize only a string's even letters?
I've tried:
str = "apples"; str.capitalize
"abcdefgh".gsub /..?/, &:capitalize
=> "AbCdEfGh"
"abcdefgh".gsub /(?!^)..?/, &:capitalize
=> "aBcDeFgH"
Use string.gsub! instead of string.gsub if you want to modify your original string.
string.scan(/..?/).map(&:capitalize) * ''