3

I have a complex string with an unknown number of letters, numbers, and characters. For instance

"a83bal*av31-ab+asf3/af1124-xxx.afe-100/araw31/31*xxx.g41.abt.eta+131as*dfa"

The goal is to find the string xxx and extract everything after it until a +, -, * , /. Thus I should be able to extract xxx.afe and xxx.g41.abt.eta Finding the string is simple string:find("xxx") the trouble I am having is gathering all of the information following the "xxx" until one of the operators. I have tried things like

string:match("xxx.(.+[%+%-%*%/])")

and several variations of the above, but there is something I am missing.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
Miles Engel
  • 131
  • 2

2 Answers2

1
str = "a83bal*av31-ab+asf3/af1124-xxx.afe-100/araw31/31*xxx.g41.abt.eta+131as*dfa"    
for m in str:gmatch("[+%-*/](xxx.-)[+%-*/]") do
    print(m)
end

Note that - has special meaning even inside char-set (e.g, [1-9a-f]), so it must be escaped, or be put in the beginning or end of the char-set, i.e, [-+*/].

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
1

You can use the negated character class to match ([^-+*/]):

for sWord in str:gmatch "xxx%.([^-+*/]+)" do
    -- do things to sWord
end

Note that, looking at your code snippet; you're using variable named string. string stores the metatable for the general string data types in lua environment. Please avoid using variable names like that.

hjpotter92
  • 78,589
  • 36
  • 144
  • 183