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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:27:50+00:00 2026-06-12T14:27:50+00:00

Consider that you have the following string: id: 1 name: Joe age: 27 id:

  • 0

Consider that you have the following string:

id: 1 name: Joe age: 27 id: 2 name: Mary age:22

And you want to extract every token after “age:” BUT NOT the string “age:” itself.

So I want my Matcher‘s group() to return 27 and 22 and not “age: 27” and “age:22”

Is there a way to specify this instruction in the Java Regex syntax, which seems quite different than that in Perl, where I learned my Regex basics?

This is my code:

import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class RegExTest 
{
    public static void main(String[] args) 
    {
        Pattern namePtrn = Pattern.compile("age: *\\w*");

        String data = "id: 1 name: Joe age:27 id: 2 name: Mary age:22";

        Matcher nameMtchr = namePtrn.matcher(data);

        while(nameMtchr.find())
        {
            String find = nameMtchr.group();

            System.out.println ("\t" + find);
        }
    }
}

In Perl I can use {} to limit the portion of the pattern that I want extracted

while($text =~ m/(age:{\w+})/g)
{
      my $find = $1;

      if($find)
      {
          print "\nFIND = ".$find;
      }
}

would return

FIND = 27
FIND = 22

and if I put {} around age like

while($text =~ m/({age:\w+})/g)

it would return

FIND = age: 27
FIND = age:22

So I am looking for something like Perl’s {} but in Java.

  • 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-12T14:27:51+00:00Added an answer on June 12, 2026 at 2:27 pm

    If you use Matcher.group(1) instead of Matcher.group() you can capture the pattern minus ‘age:’:

    String data = "id: 1 name: Joe age:27 id: 2 name: Mary age:22";
    Pattern namePtrn = Pattern.compile("age:(\\w+)");
    Matcher nameMtchr = namePtrn.matcher(data);
    
    while (nameMtchr.find()) {
       String find = nameMtchr.group(1);
       System.out.println("\t" + find);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following example: interface IPropertyCollection { public MethodWrapper GetPropertySetterByName(string name); //<<-- I want
I want to convert a string to a class name. Imagine that I have
Consider the following scenario We have a simple database that involves two entities: user
Consider following scenario: I have RESTful URL /articles that returns list of articles user
Let's consider we have QWidget that contains QTableWidget (only). So we want to resize
Consider the following code: [Serializable] public class Human { public string Name { get;
Consider that I have the following javascript that do a post: $.post("/MyController/SomeAction", { myParam:
Consider I have a JSON string of the following format: How do I parse
Consider I have the following classes as ViewModels public class Address { public String
Consider the following Mongoid Model class Doc include Mongoid::Document field :name, type: String embeds_many

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.