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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:59:52+00:00 2026-05-22T23:59:52+00:00

How would I construct a regular expression to find all words that end in

  • 0

How would I construct a regular expression to find all words that end in a string but don’t begin with a string?

e.g. Find all words that end in ‘friend’ that don’t start with the word ‘girl’ in the following sentence:

“A boyfriend and girlfriend gained a friend when they asked to befriend them”

The items in bold should match. The word ‘girlfriend’ should not.

  • 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-22T23:59:53+00:00Added an answer on May 22, 2026 at 11:59 pm

    Off the top of my head, you could try:

    \b             # word boundary - matches start of word
    (?!girl)       # negative lookahead for literal 'girl'
    \w*            # zero or more letters, numbers, or underscores
    friend         # literal 'friend'
    \b             # word boundary - matches end of word
    

    Update

    Here’s another non-obvious approach which should work in any modern implementation of regular expressions:

    Assuming you wish to extract a pattern which appears within multiple contexts but you only want to match if it appears in a specific context, you can use an alteration where you first specify what you don’t want and then capture what you do.

    So, using your example, to extract all of the words that either are or end in friend except girlfriend, you’d use:

    \b               # word boundary
    (?:              # start of non-capture group 
      girlfriend     # literal (note 1)
    |                # alternation
      (              # start of capture group #1 (note 2)
        \w*          # zero or more word chars [a-zA-Z_]
        friend       # literal 
      )              # end of capture group #1
    )                # end of non-capture group
    \b
    

    Notes:

    1. This is what we do not wish to capture.
    2. And this is what we do wish to capture.

    Which can be described as:

    • for all words
    • first, match ‘girlfriend’ and do not capture (discard)
    • then match any word that is or ends in ‘friend’ and capture it

    In Javascript:

    const target = 'A boyfriend and girlfriend gained a friend when they asked to befriend them';
    
    const pattern = /\b(?:girlfriend|(\w*friend))\b/g;
    
    let result = [];
    let arr;
    
    while((arr=pattern.exec(target)) !== null){
      if(arr[1]) {
        result.push(arr[1]);
      }
    }
    
    console.log(result);
    

    which, when run, will print:

    [ 'boyfriend', 'friend', 'befriend' ]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to construct a regular expression that matches any letter (including accented
I would like to construct a query that displays all the results in a
I have a polygon soup of triangles that I would like to construct a
Can somebody help me construct this regular expression, please... Given the following strings... "April
What would be the quickest way to construct a Python binding to a C
I would like to cross-reference construct a distance chart similar to the one here
I'm looking for applications and programming language constructs to search for a regular expression
Regular expressions are often pointed to as the classical example of a language that
I would need some basic vector mathematics constructs in an application. Dot product, cross
How would you call the constructor of the following class in these three situations:

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.