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

The Archive Base Latest Questions

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

Pattern pattern = Pattern.compile((\\S+)\\s+(.+?)\\s+(\\S+)); Matcher matcher = pattern.matcher(IA HEART RATE 184); So my question

  • 0
Pattern pattern = Pattern.compile("(\\S+)\\s+(.+?)\\s+(\\S+)");
Matcher matcher = pattern.matcher("IA HEART RATE 184");

So my question is why does the pattern above capture HEART RATE in group 2. Shouldn’t ‘.+’ match one or more characters including a space char so shouldn’t group 2 capture HEART because it is followed by a space and that should match (.+?)\s+ correct ? So why is it matching HEART RATE?

P.S. I was using matches()

  • 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-10T09:57:37+00:00Added an answer on June 10, 2026 at 9:57 am

    You haven’t shown how you’re using matcher, but I’m guessing that you’re using its matches() method when what you really want is its find() method. matches() needs to match the pattern against the entire string, and the only way to do that is for group 2 to be HEART RATE. (If group 2 were just HEART, then group 3 would be RATE, and there would be a trailing  184 not matched by anything in the pattern.) If you were using find(), then group 2 would just be HEART, because that’s the minimum substring necessary that would allow the pattern to match part of the string.

    You also may be misunderstanding what +? means. It tries to match as little as possible, while still resulting in a match overall. (.+?)\s+ is perfectly capable of matching HEART RATE ; it’s just that it will prefer to match HEART , as long as that doesn’t prevent the rest of the pattern from matching.

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

Sidebar

Related Questions

Pattern pattern = Pattern.compile(a?); Matcher matcher = pattern.matcher(a); while(matcher.find()){ System.out.println(matcher.start()+[+matcher.group()+]+matcher.end()); } Output : 0[a]1
My code : Pattern pattern = Pattern.compile(a?); Matcher matcher = pattern.matcher(ababa); while(matcher.find()){ System.out.println(matcher.start()+[+matcher.group()+]+matcher.end()); }
Pattern pattern = Pattern.compile(<a>([a-zA-Z]+)</a>) Matcher matcher = pattern.matcher(<a>Text</a>); matcher.find() String str = matcher.group(); I
I fairly frequently match strings against regular expressions. In Java: java.util.regex.Pattern.compile(\w+).matcher(this_is).matches Ouch. Scala has
Pattern p2 = Pattern.compile(.*); Matcher m2 = p2.matcher(true); System.out.println(m2.matches() + [ + m2.group() +
Pattern p = Pattern.compile((ma)|([a-zA-Z_]+)); Matcher m = p.matcher(ma); m.find(); System.out.println(1 + m.group(1) + );
For example: Pattern pattern = Pattern.compile(a(.*)b); Matcher matcher = pattern.matcher(a19203b); matcher.find(); System.out.println(matcher.group()); This prints
Pattern patronValidity = Pattern.compile(Validity: \\[(.*?)\\]); Matcher matcherValidity = patronValidity.matcher(strCert); if(matcherValidity.find()){ System.out.println(matcherValidity.group(1)); } I'm using
I have a function that uses Pattern#compile and a Matcher to search a list
File file = new File(file-type-string-i-want-2000-01-01-01-01-01.conf.gz); Matcher matcher = pattern.compile(\\-(.*)\\-\\d{4})).matcher(fileName); StringBuilder sb = new StringBuilder();

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.