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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:35:28+00:00 2026-06-15T01:35:28+00:00

How can I match a pattern in a string that has linebreaks? e.g. requisition({title:my

  • 0

How can I match a pattern in a string that has linebreaks?
e.g.

requisition({"title":"my json",
             "items" : [{ "A": "a",
                          "B": "b"}
                       ]
            })

I Want to catch this only

{"title":"my json",
             "items" : [{ "A": "a",
                          "B": "b"}
                       ]
}

I tried something like

String pattern = ".*(\\{.*\\}).*";
Pattern r = Pattern.compile(pattern, Pattern.DOTALL);

But no sucess. Any sugestions?

Just to make more clear. This is my input

  • 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-15T01:35:29+00:00Added an answer on June 15, 2026 at 1:35 am

    The problem with a single JSON object as the input is simple: your first .* is greedy. So it consumes everything until the last { that is still followed by a }. If you made that .* ungreedy (or left it out), you should get the full JSON object:

    String pattern = ".*?(\\{.*\\}).*";
    

    But you can (and should) leave out the beginning and trailing repetitions completely:

    String pattern = "\\{.*\\}";
    

    Then you don’t even need to capture anything. Note that this has to be used with find instead of matches.

    However, your input has multiple JSON objects. And this is where you get problems with regular expressions. Some engines support constructs that allow correct nesting of brackets (to check which ones actually belong together). But those regexes can easily get ugly and not maintainable.

    You are better off, walking the string manually, and keeping count of the current nesting level. Whenever you get back to the top-level you just cut off a substring (from the corresponding opening bracket to your current position).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I match a pattern in regex that can contain anything (letters,numbers,...) but
I am trying to match a string that has the form: abcd:vxyz That is:
I know i can match numbers with Pattern.compile(\\d*); But it doesn't handle the long
How can I match a field in the MySql-Database with a regular expression that
This page has a great example using REGEXP to do pattern matching. The problem
I've got this pattern which I use with preg_match and I can't figure where
For a pattern looking like this: <VirtualHost 127.0.0.1:81> Directive1 Directive2 </VirtualHost> I want to
The file below, can be setup using any JPG file from PhotoShop that has
I've searched the questions but can't find an answer. I need a pattern that,
I'm struggling with a regex pattern that will pull out text from a string

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.