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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:18:10+00:00 2026-06-17T09:18:10+00:00

Here I am using regex to catch first 15 characters to match, and while

  • 0

Here I am using regex to catch first 15 characters to match, and while using substring I have to use (0,matcher.start()) only wherein I should get 15 only, kindly help me out.

   String test = "hello world this is example";
        Pattern p = Pattern.compile(".{15}");

    //can't change the code below
    //can only make changes to pattern 


        Matcher m=p.matches(test);
        matcher.find(){
            String string = test.substring(0, m.start());
        }

    //here it is escaping the first 15 characters but I need them
    //the m.start() here is giving 0 but it should give 15
  • 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-17T09:18:11+00:00Added an answer on June 17, 2026 at 9:18 am

    If possible, I agree with @jlordo’s comment: Use String string = test.substring(0, 15);

    If you are forced to pass through the bottom code snippet which you marked as unchangeable, there is a workaround.
    (Well that depends… If you are stuck with an unchangeable code snippet does not even compile… You’re gonna have a bad time)

    If you really need a regex that always returns 15 for m.start() you can use the regex lookbehind concept.

            String test = "hello world this is example";
            Pattern p = Pattern.compile("(?<=.{15}).");
    
            Matcher m=p.matcher(test);
            m.find();
            System.out.println(m.start());
    

    Granted that the test input string is at least 16 characters long, this will always return 15 for m.start().
    The regex is supposed to be read as “Any character (the last .) preceded by (the (?<=) lookbehind operator) exactly 15 any characters (the .{15})”.

    (?<=foo) is a lookbehind operator specifying that whatever regex comes after must be preceded by “foo”.
    E.g. the regex: (?<=foo)bar
    Will match the bar in: foobar
    But not the bar in: wunderbar

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

Sidebar

Related Questions

I'm trying to match a URL in a string, using regex from here: Regular
I'm trying to get all words inside a string using Boost::regex in C++. Here's
I’m trying to validate input by using egrep and regex.Here is the line from
Using Rational ClearCase v. 7.0.1.1 with UCM, I have a problem here when using
Here's where I am: I started with an array...cleaned it up using 'regex'. Now
Using Regex in .Net I will have a set of data that comes in
I'm using regex to match all non-quoted property names in my json files. Eclipse
I need to catch the content of href using regex. For example, when I
Good day stack overflow. I'm a noob in using regex and here is my
Can anyone tell me what ?= does when using regex? Here is an example

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.