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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:54:38+00:00 2026-06-10T00:54:38+00:00

I’m trying to capture a string up until a certain word that is within

  • 0

I’m trying to capture a string up until a certain word that is within some group of words.

I only want to capture the string up until the FIRST instance of one of these words, as they may appear many times in the string.

For example:

Group of words: (was, in, for)

String = “Once upon a time there was a fox in a hole”;
would return “Once upon a time there”

Thank you

  • 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-10T00:54:39+00:00Added an answer on June 10, 2026 at 12:54 am

    What you need is called a Lookahead. The exact regex for your situation is:

    /^.+?(?=(?:was)|(?:in)|(?:for))/
    

    Anyway, the ^ matches the beginning of the string, .+? is a lazy match(it will match the shortest possible string), (?= … ) means "followed by" and (?: … ) is a noncapturing group – which may or may not be necessary for you.

    For bonus points, you should probably be using word boundaries to make sure you’re matching the whole word, instead of a substring ("The fox wasn’t" would return "The fox "), and a leading space in the lookahead to kill the trailing space in the match:

    /^.+?(?=\s*\b(?:was)|(?:in)|(?:for)\b)/
    

    Where \s* matches any amount of white space (including none at all) and \b matches the beginning or end of a word. It’s a Zero-Width assertion, meaning it doesn’t match an actual character.

    Or, in Java:

    Pattern p = Pattern.compile("^.+?(?=\\s*\\b(?:was)|(?:in)|(?:for)\\b)");
    

    I think that will work. I haven’t used it, but according to the documentation, that exact string should work. Just had to escape all the backslashes.

    Edit

    Here I am, more than a year later, and I just realized the regex above does not do what I thought it did at the time. Alternation has the highest precedence, rather than the lowest, so this pattern is more correctly:

    /^.+?(?=\s*\b(?:was|in|for)\b)/

    Compare this new regex to my old one. Additionally, future travelers, you may wish to capture the whole string if no such breaker word exists. Try THIS on for size:

    /^(?:(?!\s*\b(?:was|in|for)\b).)*/
    

    This one uses a NEGATIVE lookahead (which asserts a match that fails the pattern). It’s possibly slower, but it still does the job. See it in action here.

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

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka

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.