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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T07:04:06+00:00 2026-05-31T07:04:06+00:00

I want to construct a regex, that matches either ‘ or and then matches

  • 0

I want to construct a regex, that matches either ' or " and then matches other characters, ending when a ' or an " respectively is matched, depending on what was encountered right at the start. So this problem appears simple enough to solve with the use of a backreference at the end; here is some regex code below (it’s in Java so mind the extra escape chars such as the \ before the "):

private static String seekerTwo = "(['\"])([a-zA-Z])([a-zA-Z0-9():;/`\\=\\.\\,\\- ]+)(\\1)";

This code will successfully deal with things such as:

"hello my name is bob"
'i live in bethnal green'

The trouble comes when I have a String like this:

"hello this seat 'may be taken' already"

Using the above regex on it will fail on the initial part upon encountering ' then it would continue and successfully match 'may be taken'… but this is obviously insufficient, I need the whole String to be matched.

What I’m thinking, is that I need a way to ignore the type of quotation mark, which was NOT matched in the very first group, by including it as a character in the character set of the 3rd group. However, I know of no way to do this. Is there some sort of sneaky NOT backreference function or something? Something I can use to reference the character in the 1st group that was NOT matched?? Or otherwise some kind of solution to my predicament?

  • 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-31T07:04:08+00:00Added an answer on May 31, 2026 at 7:04 am

    This can be done using negative lookahead assertions. The following solution even takes into account that you could escape a quote inside a string:

    (["'])(?:\\.|(?!\1).)*\1
    

    Explanation:

    (["'])    # Match and remember a quote.
    (?:       # Either match...
     \\.      # an escaped character
    |         # or
     (?!\1)   # (unless that character is identical to the quote character in \1)
     .        # any character
    )*        # any number of times.
    \1        # Match the corresponding quote.
    

    This correctly matches "hello this seat 'may be taken' already" or "hello this seat \"may be taken\" already".

    In Java, with all the backslashes:

    Pattern regex = Pattern.compile(
        "([\"'])   # Match and remember a quote.\n" +
        "(?:       # Either match...\n" +
        " \\\\.    # an escaped character\n" +
        "|         # or\n" +
        " (?!\\1)  # (unless that character is identical to the matched quote char)\n" +
        " .        # any character\n" +
        ")*        # any number of times.\n" +
        "\\1       # Match the corresponding quote", 
        Pattern.COMMENTS);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple problem: I want to construct a regex that matches a
i want to realize a construct in MS SQL that would look like this
I want to construct a regular expression that will, for every line of text
I want to construct a transact sql script that will stop specified people from
I want to construct a context menu with a menu item for selecting a
I want to construct a string with an array of objects for (int n=0;
In a Windows batch file, I want to construct a classpath of .jar files
I want to use the DbProvider class to construct a generic DAL component. This
I'm trying to construct a regex to screen valid part and/or serial numbers in
I have a shell script that constructs an awk program as a string then

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.