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

The Archive Base Latest Questions

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

I am new to java regex.Please help me. Consider the below paragraph, Paragraph :

  • 0

I am new to java regex.Please help me.
Consider the below paragraph,

Paragraph :

            Name abc
            sadghsagh
            hsajdjah Name
            ggggggggg
            !!!
            Name ggg
            dfdfddfdf Name
            !!!
            Name hhhh
            sahdgashdg Name
            asjdhjasdh
            sadasldkalskd
            asdjhakjsdhja
            !!!

i need to split the above paragraph as blocks of text starting with Name and ending with !!! . Here I dont want to use !!! as the only delimiter to split the paragraph.I need to include the starting sequence (Name) also in my regex.

ie., my result api should looks like SplitAsBlocks(“Paragraph”,”startswith Name”,”endswith
!!!”)

How to achieve this ,please anyone help me …

Now i want the same output as Brito given …but here i have added Name after “hsajdjah”.Here it split the text as beow :

Name
ggggggggg
!!!

but i need

Name abc
sadghsagh
hsajdjah Name
ggggggggg
!!!

that is i have to match up Name which is at the starting of the line ,not in the middle .

please suggest me …

Bart …see the below input case for your code …

i need to split the following using ur API with parameter start => Name and end => !
But the output varies ..i have only 3 blocks starts with Name and ends with ! .
i have attached the output also .

String myInput =    "Name hhhhh class0"+ "\n"+
                     "HHHHHHHHHHHHHHHHHH"+ "\n"+
                     "!"+ "\n"+
                     "Name TTTTT TTTT"+ "\n"+
                     "GGGGGG UUUUU IIII"+ "\n"+
                     "!"+ "\n"+
                     "Name JJJJJ WWWW"+ "\n"+
                     "IIIIIIIIIIIIIIIIIIIII"+ "\n"+
                     "!"+ "\n"+
                     "RRRRRRRRRRR TTTTTTTT"+ "\n"+
                     "HHHHHH"+ "\n"+
                     "JJJJJ 1 Name class1"+ "\n"+
                     "LLLLL 5 Name class5"+ "\n"+
                     "!"+ "\n"+
                     "OOOOOO HHHH FFFFFF"+ "\n"+
                     "service 0 Name class12"+ "\n"+
                     "!"+ "\n"+
                     "JJJJJ YYYYYY 3/0"+ "\n"+
                     "KKKKKKK"+ "\n"+
                     "UUU UUU UUUUU"+ "\n"+
                     "QQQQQQQ"+ "\n"+
                         "!";
    String[] tokens = tokenize(myInput, "Name", "!");
    int n = 0;
    for(String t : tokens) {
        System.out.println("---------------------------\n"+(++n)+"\n"+t);
    }

OutPut :

---------------------------
1
Name hhhhh class0
HHHHHHHHHHHHHHHHHH
!
---------------------------
2
Name TTTTT TTTT
GGGGGG UUUUU IIII
!
---------------------------
3
Name JJJJJ WWWW
IIIIIIIIIIIIIIIIIIIII
!
---------------------------
4
Name class1
LLLLL 5 Name class5
!
---------------------------
5
Name class12
!

Here i need to have only the Name at the starting of the line not at the middle …
How to add regex for this …

  • 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-12T22:06:32+00:00Added an answer on May 12, 2026 at 10:06 pm

    Try:

    import java.util.*;
    import java.util.regex.*;
    
    public class Main { 
    
        public static String[] tokenize(String text, String start, String end) {
            // old line:
            //Pattern p = Pattern.compile("(?s)"+Pattern.quote(start)+".*?"+Pattern.quote(end));
            // new line:
            Pattern p = Pattern.compile("(?sm)^"+Pattern.quote(start)+".*?"+Pattern.quote(end)+"$");
    
            Matcher m = p.matcher(text);
            List<String> tokens = new ArrayList<String>();
            while(m.find()) {
                tokens.add(m.group());
            }
            return tokens.toArray(new String[]{});
        }
    
        public static void main(String[] args) {
            String text = "Name abc" + "\n" +
                "sadghsagh"          + "\n" +
                "hsajdjah Name"      + "\n" +
                "ggggggggg"          + "\n" +
                "!!!"                + "\n" +
                "Name ggg"           + "\n" +
                "dfdfddfdf Name"     + "\n" +
                "!!!"                + "\n" +
                "Name hhhh"          + "\n" +
                "sahdgashdg Name"    + "\n" +
                "asjdhjasdh"         + "\n" +
                "sadasldkalskd"      + "\n" +
                "asdjhakjsdhja"      + "\n" +
                "!!!";
            String[] tokens = tokenize(text, "Name", "!!!");
            int n = 0;
            for(String t : tokens) {
                System.out.println("---------------------------\n"+(++n)+"\n"+t);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 284k
  • Answers 284k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer According to java ee 5 api ExpressionFactory provides a method… May 13, 2026 at 4:34 pm
  • Editorial Team
    Editorial Team added an answer If ClassA has a reference to FormA (through the event… May 13, 2026 at 4:34 pm
  • Editorial Team
    Editorial Team added an answer Based on the answer by Benoît I have come up… May 13, 2026 at 4:34 pm

Related Questions

In java i would like to read a file line by line and print
I am new to Selenium. I generated my first java selenium test case and
I am having a problem with my regular expression: <a.*href=[\'](.*?)[\'].*>(.*?)</a> . It is, as
I am using split() to tokenize a String separated with * following this format:

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.