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 8997425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:53:16+00:00 2026-06-15T23:53:16+00:00

I am trying to build a regex that stops at the first occurrence. I

  • 0

I am trying to build a regex that stops at the first occurrence. I know I can make it non-greedy by putting ?.

Consider a string:

"This is sample text located at first line and located at second line."

Here, I am searching for pattern1 using pattern2.

  • pattern1 is "text"
  • pattern2 is "located at"

In the above string, I want to extract "text", and my search pattern is "located at", so I am using the following regex:

/is.*sample(.*)located at?/

How do I make located at non-greedy? I am using http://rubular.com/ to verify my regex.

  • 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-06-15T23:53:17+00:00Added an answer on June 15, 2026 at 11:53 pm

    Your regex isn’t correct.

    If you want a single “word” that occurs before the first “located at”, you could use:

    "This is sample text located at first line and located at second line."[/(\S+)\s+located at/, 1]
    => "text"
    

    I’m defining “word” to mean non-whitespace characters using \S, so punctuation and numbers are going to be included with the alpha characters. Other classes could be used, such as \w if you want [A-Za-z0-9_]. Otherwise use [a-z] like:

    "This is sample text located at first line and located at second line."[/([a-z]+)\s+located at/i, 1]
    => "text"
    

    If you want any text that occurs between “sample” and the first “located at”, you could use:

    "This is sample text located at first line and located at second line."[/sample\s+(.+?)\s+located at/, 1]
    => "text"
    

    In your pattern /test.*sample(.*)located at?/, you’re using multiple .*, which mean zero-or-more of anything (but, not really anything depending on the context but that’s deeper than we need to go right now). That “more” is the part you’re colliding with, because it’s greedy. And, because you use that twice, it’s doubly greedy. You could use the “non-greedy” variant by adding ?, but it still wouldn’t work right because you’re giving the regex engine too much rope to play with. My patterns tighten that all up, reducing the need to use the ? modifier in the first two.

    My third example needs it because, again, .+ would have been greedy and it needed to be moderated.

    Finally, at? in your pattern isn’t applying ? to modify the .*, it’s acting on the preceeding t, causing the engine to thing “zero-or-one ‘t’ must be found”, which isn’t what you want, because that would match “a” or “at”.

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

Sidebar

Related Questions

I'm trying to build a regex that joins numbers in a string when they
I am trying to build a regex function that will remove any non alpha
I'm trying to build a regex that can look for names that contain apostrophes
I've been whacking on this regex for a while, trying to build something that
I'm trying to build a simple User Template system so that my users can
I'm trying build a regex that will replace any characters not of the format:
I am trying to build a regex that should match for example. b abab
I'm trying to build a regex to determine if a string contains a byte
I'm trying to build a python class that parses a string and if it
I'm trying to build a regex from a string object, which happens to be

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.