3

I need some help with creating my pattern. I've got the basic parts done, but one issue remains.

Let's say I have a string as follows:

John: I can type in :red:colour! :white:Not in the same :red:wo:green:rd :white:though :(

I have this code setup to separate the colors from the actual value:

line = "John: I can type in :red:colour! :white:Not in the same :red:wo:green:rd :white:though :("

for token in string.gmatch(line, "%s?[(%S)]+[^.]?") do
   for startpos, token2, endpos in string.gmatch(token, "()(%b::)()") do
      print(token2)
      token = string.gsub(token, token2, "")
   end
   print(token)
end

Will output:

John: 
I 
can 
type 
in 
:red:
colour! 
:white:
Not 
in 
the 
same 
:red:
:green:
word 
:white:
though 
:(

When I want it to print out:

John: 
I 
can 
type 
in 
:red:
colour! 
:white:
Not 
in 
the 
same 
:red:
wo
:green:
rd 
:white:
though 
:(

Any help would be greatly appreciated.

2 Answers 2

2

The following code will give your desired output:

for token in line:gmatch( "(%S+)" ) do
  if not token:match( "(:%w-:)([^:]+)" ) then
    print(token)
  else
    for col, w in token:gmatch( "(:%w-:)([^:]+)" ) do
      print( col )
      print( w )
    end
  end
end

Though, it will fail for a string such as:

in the sa:yellow:me:pink:long-Words!
Sign up to request clarification or add additional context in comments.

2 Comments

This does the job partially, however when someone types something such as "00:20:14", the first two zeros disappear. Any help would be greatly appreciated again :)
@user1773027 If you're sure that you'll only be using colours in :red: format, then you can use (:%a-:) in place of (:%w-:).
0

A more generic solution works:

line = "John: I can type in :red:colour! :white:Not in the same :red:wo:green:rd :white:though :("

for i in string.gmatch(line ,"%S+") do
    if (i:match(":%w+")) then
        for k,r in string.gmatch(i,"(:%w+:)(%w+[^:]*)") do
            print(k)
            print(r)
        end
    else
        print(i)
    end
end

Will also work for the string: "in the sa:yellow:me:pink:long-Words!"

Comments

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.