Get Character Groups from String

Is there a way (without looping through an entire string) to convert a given string to character groups? Each punctuation mark would be in it’s own place, and each word in it’s own place, like so:

Original string:

“Hello, world!”

Converted array of strings:

“Hello”, “,” " ", “world”, “!”

Thanks for any help!

C

You could probably use string.match() for it and regular expressions.

That’s what I’m trying to figure out - I’ve already done only the words, and I’ve already done only the punctuation. Technically I could do words and then punctuation, but they need to be in order.

Here’s words -

[lua]

– str is any string

for w in str:gmatch("[%w_]+") do

    print(w)

end

[/lua]

And here’s non-words -

[lua]

for w in str:gmatch("%A") do

    print(w)

end

[/lua]

But if I do a table.insert for that, it’s not in order - “ABC…def…GHI!!!” results in “ABC” “def” “GHI” “.” “.” “.” “.” “.” “.” “!” “!” “!”

Does anyone know how to do it in order?

It would appear that there might be an “or” character for gmatch, but I’ve not found it. If there were an “or” character, you could just check for a word or a non-word.

Thanks!

Caleb

You could probably use string.match() for it and regular expressions.

That’s what I’m trying to figure out - I’ve already done only the words, and I’ve already done only the punctuation. Technically I could do words and then punctuation, but they need to be in order.

Here’s words -

[lua]

– str is any string

for w in str:gmatch("[%w_]+") do

    print(w)

end

[/lua]

And here’s non-words -

[lua]

for w in str:gmatch("%A") do

    print(w)

end

[/lua]

But if I do a table.insert for that, it’s not in order - “ABC…def…GHI!!!” results in “ABC” “def” “GHI” “.” “.” “.” “.” “.” “.” “!” “!” “!”

Does anyone know how to do it in order?

It would appear that there might be an “or” character for gmatch, but I’ve not found it. If there were an “or” character, you could just check for a word or a non-word.

Thanks!

Caleb