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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:15:39+00:00 2026-06-15T09:15:39+00:00

I know I can exclude outside characters in a string using look-ahead and look-behind,

  • 0

I know I can exclude outside characters in a string using look-ahead and look-behind, but I’m not sure about characters in the center.

What I want is to get a match of ABCDEF from the string ABC 123 DEF.

Is this possible with a Regex string? If not, can it be accomplished another way?

EDIT

For more clarification, in the example above I can use the regex string /ABC.*?DEF/ to sort of get what I want, but this includes everything matched by .*?. What I want is to match with something like ABC(match whatever, but then throw it out)DEF resulting in one single match of ABCDEF.

As another example, I can do the following (in sudo-code and regex):

string myStr = "ABC 123 DEF";
string tempMatch = RegexMatch(myStr, "(?<=ABC).*?(?=DEF)"); //Returns " 123 "
string FinalString = myStr.Replace(tempMatch, ""); //Returns "ABCDEF". This is what I want

Again, is there a way to do this with a single regex string?

  • 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-15T09:15:40+00:00Added an answer on June 15, 2026 at 9:15 am

    Since the regex replace feature in most languages does not change the string it operates on (but produces a new one), you can do it as a one-liner in most languages. Firstly, you match everything, capturing the desired parts:

    ^.*(ABC).*(DEF).*$
    

    (Make sure to use the single-line/”dotall” option if your input contains line breaks!)

    And then you replace this with:

    $1$2
    

    That will give you ABCDEF in one assignment.

    Still, as outlined in the comments and in Mark’s answer, the engine does match the stuff in between ABC and DEF. It’s only the replacement convenience function that throws it out. But that is supported in pretty much every language, I would say.

    Important: this approach will of course only work if your input string contains the desired pattern only once (assuming ABC and DEF are actually variable).

    Example implementation in PHP:

    $output = preg_replace('/^.*(ABC).*(DEF).*$/s', '$1$2', $input);
    

    Or JavaScript (which does not have single-line mode):

    var output = input.replace(/^[\s\S]*(ABC)[\s\S]*(DEF)[\s\S]*$/, '$1$2');
    

    Or C#:

    string output = Regex.Replace(input, @"^.*(ABC).*(DEF).*$", "$1$2", RegexOptions.Singleline);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know you can not set a key value dynamically, but what about the
I have a query that I know can be done using a subselect, but
You can know if the event stack is empty calling the gtk.events_pending() method, but
I know you can share control easily with master page. But what if I
I'd like to know if I can exclude a JAR file (i.e. the corresponding
I know that I can create xml files programmatically by using DOM api in
I am not sure if the title makes any sense but here is the
Anybody know how can I exclude only the first letter of a get_the_content() function
I want to know can we have a JPanel with a Layout other than
This is something I know can be done somehow , because I've done it

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.