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

  • Home
  • SEARCH
  • 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 1036119
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:39:03+00:00 2026-05-16T14:39:03+00:00

I have a string like hello /world today/ I need to replace /world today/

  • 0

I have a string like hello /world today/

I need to replace /world today/ with /MY NEW STRING/

Reading the manual I have found

newString = string.match("hello /world today/","%b//")

which I can use with gsub to replace, but I wondered is there also an elegant way to return just the text between the /, I know I could just trim it, but I wondered if there was a pattern.

  • 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-16T14:39:03+00:00Added an answer on May 16, 2026 at 2:39 pm

    Try something like one of the following:

    • slashed_text = string.match("hello /world today/", "/([^/]*)/")
    • slashed_text = string.match("hello /world today/", "/(.-)/")
    • slashed_text = string.match("hello /world today/", "/(.*)/")

    This works because string.match returns any captures from the pattern, or the entire matched text if there are no captures. The key then is to make sure that the pattern has the right amount of greediness, remembering that Lua patterns are not a complete regular expression language.

    The first two should match the same texts. In the first, I’ve expressly required that the pattern match as many non-slashes as possible. The second (thanks lhf) matches the shortest span of any characters at all followed by a slash. The third is greedier, it matches the longest span of characters that can still be followed by a slash.

    The %b// in the original question doesn’t have any advantages over /.-/ since the the two delimiters are the same character.

    Edit: Added a pattern suggested by lhf, and more explanations.

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

Sidebar

Related Questions

I have string like {param1=foo}{param2=bar}hello world! I need to extract array of tuples (paramName,
I have a string, let's say Hello world , and I need to replace
I have a string like so string1 : Hello World, join my game: I
I have a textbox where a user puts a string like this: hello world!
I may have string like, Hello, %(name)s, how are you today, here is amount
Let's say I have a string like this: $string = hello---world; How would I
I have a string such as <html><body><div id=message>Hello World!</div></body></html> and I would like to
I am going to have a string like hello world as a hashmap key.
As the title says I have a string like this: $string = "Hello World<br>hello
Basically if I have a string that looks like this: %22Hello+World+%26+Hello+World%22 because I took

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.