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.

2 Answers 2

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, [-+*/].

Sign up to request clarification or add additional context in comments.

Comments

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.

1 Comment

string does not store the metatable for strings. It stores __index value of that metatable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.