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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T04:28:10+00:00 2026-05-15T04:28:10+00:00

I’m trying to extract snippets of dialogue from a book text. For example, if

  • 0

I’m trying to extract snippets of dialogue from a book text. For example, if I have the string

"What's the matter with the flag?" inquired Captain MacWhirr. "Seems all right to me."

Then I want to extract "What's the matter with the flag?" and "Seem's all right to me.".

I found a regular expression to use here, which is "[^"\\]*(\\.[^"\\]*)*". This works great in Eclipse when I’m doing a Ctrl+F find regex on my book .txt file, but when I run the following code:

String regex = "\"[^\"\\\\]*(\\\\.[^\"\\\\]*)*\"";
String bookText = "\"What's the matter with the flag?\" inquired Captain MacWhirr. \"Seems all right to me.\""; Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(bookText);

if(m.find())
 System.out.println(m.group(1));

The only thing that prints is null. So am I not converting the regex into a Java string properly? Do I need to take into account the fact that Java Strings have a \" for the double quotes?

  • 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-15T04:28:10+00:00Added an answer on May 15, 2026 at 4:28 am

    In a natural language text, it’s not likely that " is escaped by a preceding slash, so you should be able to use just the pattern "([^"]*)".

    As a Java string literal, this is "\"([^\"]*)\"".

    Here it is in Java:

    String regex = "\"([^\"]*)\"";
    String bookText = "\"What's the matter with the flag?\" inquired Captain MacWhirr. \"Seems all right to me.\"";
    Pattern p = Pattern.compile(regex);
    Matcher m = p.matcher(bookText);
    
    while (m.find()) {
        System.out.println(m.group(1));
    }
    

    The above prints:

    What's the matter with the flag?
    Seems all right to me.
    

    On escape sequences

    Given this declaration:

    String s = "\"";
    System.out.println(s.length()); // prints "1"
    

    The string s only has one character, ". The \ is an escape sequence present at the Java source code level; the string itself has no slash.

    See also

    • JLS 3.10.6 Escape Sequences for Character and String Literals

    The problem with the original code

    There’s actually nothing wrong with the pattern per se, but you’re not capturing the right portion. \1 isn’t capturing the quoted text. Here’s the pattern with the correct capturing group:

    String regex = "\"([^\"\\\\]*(?:\\\\.[^\"\\\\]*)*)\"";
    String bookText = "\"What's the matter?\" inquired Captain MacWhirr. \"Seems all right to me.\"";
    Pattern p = Pattern.compile(regex);
    Matcher m = p.matcher(bookText);
    
    while (m.find()) {
        System.out.println(m.group(1));
    }
    

    For visual comparison, here’s the original pattern, as a Java string literal:

    String regex = "\"[^\"\\\\]*(\\\\.[^\"\\\\]*)*\""
                                ^^^^^^^^^^^^^^^^^
                               why capture this part?
    

    And here’s the modified pattern:

    String regex = "\"([^\"\\\\]*(?:\\\\.[^\"\\\\]*)*)\""
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                        we want to capture this part!
    

    As mentioned before, though: this complicated pattern isn’t necessary for natural language text, which isn’t likely to contain escaped quotes.

    See also

    • regular-expressions.info/Grouping and backreferences
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

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 have a text area in my form which accepts all possible characters from
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Does anyone know how can I replace this 2 symbol below from the string
i got an object with contents of html markup in it, for example: string
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I have a view passing on information from a database: def serve_article(request, id): served_article

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.