Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How would I return text between two numbers?

Asked by
Azarth 3141 Moderation Voter Community Moderator
10 years ago

I have a block of text as a large string ( so I can just paste in all the questions ) , but I want to make flash cards, so each questions needs to be considered as ONE string, hence the use of gmatch to separate them, so I can create a GUI for each question. Since each of the question starts with a number and ends with the page number, I need to figure out the pattern that will return all text between those two numbers.

local str = [[

1. Why have the earliest ritual jade discs (bi) been found in tombs?
a. The Chinese believed that jade would absorb the person?s soul
b. They served as payment for entrance into the afterlife
c. The Chinese believed that jade preserved the body from decay
d. The beasts on their rims were to protect the dead from demons
Answer: c page 214

2. According to Chinese legend, what inspired Fu Xi?s invention of a pictographic writing system?
a. Visions that came to him in a dream
b. Random scratches he noticed on animal bones
c. Constellations and bird and animal footprints
d. Papyri left behind by seafaring Egyptians
Answer: c page 214
]]


for i in string.gmatch(str,"%d*%d") do --> 1, 214    2, 214 but not the text inbetween
    print(i)
end

1 answer

Log in to vote
3
Answered by
Destrings 406 Moderation Voter
10 years ago

No, the correct pattern would be

Pattern = "%d+.-%d+"

Your pattern is telling: Find 0 or more digits followed by 1 digit.

While mine tells: Find the shortest sequence of characters between two or more digits.

0
Yay, thanks. Azarth 3141 — 10y
0
what is that weird thing with the % and the d and + and dot and minus ;-; greatneil80 2647 — 5y
Ad

Answer this question