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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:16:27+00:00 2026-05-26T20:16:27+00:00

Possible Duplicate: Regex – nested patterns – within outer pattern but exclude inner pattern

  • 0

Possible Duplicate:
Regex – nested patterns – within outer pattern but exclude inner pattern

I am trying to get a substring of a string after/from a word. But I want that word to be outside of the parenthesis. For example:

something (theword other things) theword some more stuff should give me theword some more stuff instead of theword other things) theword more stuff. How can I do this in regular expressions. I am using PCRE (e.g. php, python regex engine)

Edit:

The string I am trying to use this regular expression on is a mysql statement. I am trying to remove parts until FROM part, but inner sql statements (that are in parenthesis causing problems to me).

  • 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-26T20:16:27+00:00Added an answer on May 26, 2026 at 8:16 pm

    This is a task similar to the one described in this question: Regex – nested patterns – within outer pattern but exclude inner pattern

    See my answer for why it’s not 100% possible and for a hack-ish solution that works most of the times (for shallow nesting, that is).

    Update:

    If you know that parentheses wil neither be nested, nor escaped, you could use something akin to this:

    (?<=\)|^)([^()]+)(?=\(|$)
    

    So with this as the haystack:

    something (theword other things) theword (some) more stuff
    

    Wou would end up with these snippets in capture group 1:

    something 
     theword 
     more stuff
    

    Note that there are leading/trailing spaces captured as well, to prevent them use this instead:

    (?<=\)|^)\s*([^()]+)\s*(?=\(|$)
    

    Same regex pattern, but with comments:

    (?<=\)|^) #either ")" or beginning of string
    \s* #optional leading whitespace
    ([^()]+) #any sequence of characters but "(" or ")"
    \s* #optional trailing whitespace
    (?=\(|$) #either "(" or end of string
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Regex for checking if a string has mismatched parentheses? I'm trying to
Possible Duplicate: Need a good regex to convert URLs to links but leave existing
Possible Duplicate: RegEx match open tags except XHTML self-contained tags i want to grap
Possible Duplicate: Regex for matching javadoc fragments I have files having content like /**
Possible Duplicate: Java - Regex problem I have list of URLs of types: http://www.example.com/pk/etc
Possible Duplicate: A comprehensive regex for phone number validation Greetings, I need to implement
Possible Duplicate: Escape string for use in Javascript regex I have a msg like
Possible Duplicate: What is the ultimate postal code and zip regex? I need Regex
Possible Duplicate: Regex.IsMatch vs string.Contains Which is faster, preferable and why? What the difference
Possible Duplicate: Regex to change format of all img src attributes Hi, I want

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.