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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:24:30+00:00 2026-06-17T19:24:30+00:00

I am trying to match within a selection in a JTextArea and I am

  • 0

I am trying to match within a selection in a JTextArea and I am using Matcher.region() to define the boundary of my match:

JTextComponent t;

Pattern p = Pattern.compile("string", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
Matcher m = p.matcher(t.getText()).region(t.getSelectionStart(), t.getSelectionEnd());
m.useAnchoringBounds(false);

if(m.find()) {
  System.out.println("Found match from " + m.start() + " to " + m.end());
}
else {
  System.out.println("No match found");
}

The above works as expected and it will find the first match within the region – if no match is found then it doesn’t find a match.

However, I am trying to loop through the matches within the region (search and replace type function) and if I specify a start position to find() that is within the region then it matches outside of the region:

int cPos = m.regionStart();
if (m.find(cPos) || m.find(m.regionStart())) {
  System.out.println("Found match from " + m.start() + " to " + m.end());
  cPos = m.end();
}
else {
  System.out.println("No match found");
}

Is this a bug or am I breaking the region if I specify a start position – even if it is contained within the region?

  • 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-17T19:24:32+00:00Added an answer on June 17, 2026 at 7:24 pm

    From the Javadoc:

    public boolean find(int start)

    Resets this matcher and then […]

    (emphasis mine)

    • 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 string against a pattern, but there's one thing I
I'm trying to match to expresions contained within [%___%] in a string, before //
I'm trying to query a string to match within a range in the database.
I am trying to match a @ tags within this string: @sam @gt @channel:sam
I'm trying to match a label within a valid domain name using a regular
I'm trying to search for a word within string. Example match = 'File' s2
I'm trying to match usernames within a string like: user: hi, has anyone seen
I'm trying to pattern match on an exception within its definition. Is something like
I am trying to match on a string that begins with ./media I have
I am trying to match words driven from a database in a string input

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.