Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6215741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:06:37+00:00 2026-05-24T07:06:37+00:00

I have two pieces of code that break up a string based on the

  • 0

I have two pieces of code that break up a string based on the patterns.

First one:

local test = "whop^1whap^2whup"
local pattern = "%^[12]"
local lastpos = 1

for startpos, endpos in string.gmatch( test, "()"..pattern.."()" ) do


   print("pos: "..startpos..","..endpos)
   print("str: "..string.sub(test,lastpos,startpos-1))
   print("match: "..string.sub(test,startpos,endpos-1))

   lastpos = endpos

end

This one breaks up the string around ^1 or ^2. It outputs:

pos: 5,7
str: whop
match: ^1
pos: 11,13
str: whap
match: ^2

The second version is this:

local test = "whop^t1whap^02whup"
local pattern = "%^[(t1)(02)]"
local lastpos = 1

for startpos, endpos in string.gmatch( test, "()"..pattern.."()" ) do


   print("pos: "..startpos..","..endpos)
   print("str: "..string.sub(test,lastpos,startpos-1))
   print("match: "..string.sub(test,startpos,endpos-1))

   lastpos = endpos

end

This one is SUPPOSED to break up the string at ^t1 or ^02. Instead I get this:

pos: 5,7
str: whop
match: ^t
pos: 12,14
str: 1whap
match: ^0

I noticed that the first pos (5,7) is exactly the same as in the first piece of code, even though the pattern length should be 3 characters.

What am I doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T07:06:38+00:00Added an answer on May 24, 2026 at 7:06 am

    Lua patterns are not regular expressions. For example, the pattern [(t1)(02)] does not mean “match the strings ‘t1’ or ’02′”. It means “match the characters ‘(‘, ‘t’, ‘1’, ‘0’, ‘2’, or ‘)'”. It is the lack of features like this in Lua patterns that make them much easier to implement (and therefore makes the Lua standard library smaller).

    You have reached the limits of the parse-ability of Lua patterns. If you need regular expressions, I suggest you download a Lua module that implements them.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have these two pieces of code, wich one is more readable? foreach decimal
I have two very similar pieces of ASP.NET code that send a file in
I have two pieces of code that I'm using to learn about multiprocessing in
I have these two pieces of code that could possibly be run at the
I have two pieces of code in Jsfiddle. One works and the other one
I have two pieces of code which are very similar in that they register
Okay so I have two import pieces of code involved in this. This first
I have two pieces of C++ code running on 2 different cores. Both of
I have the following two pieces of code: public class C { public void
I have two essentially separate applications for configuring two pieces of hardware sold by

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.