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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:44:47+00:00 2026-06-14T21:44:47+00:00

in the following, i need to get: String regex = Item#: <em>.*</em>; String content

  • 0

in the following, i need to get:

String regex = "Item#: <em>.*</em>";
String content = "xxx Item#: <em>something</em> yyy";

Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(content); 

if( matcher.find() ) {
    System.out.println(matcher.group());
}

it will print:

Item#: <em>something</em>

but i just need the value “something”.

i know i can use .substring(begin,end) to get the value,
but is there another way which would be more elegant?

  • 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-14T21:44:48+00:00Added an answer on June 14, 2026 at 9:44 pm

    It prints the whole string because you have printed it. matcher.group() prints the complete match. To get specific part of your matched string, you need to change your Regex to capture the content between the tag in a group: –

    String regex = "Item#: <em>(.*?)</em>";
    

    Also, use Reluctant quantifier(.*?) to match the least number of characters before an </em> is encountered.

    And then in if, print group(1) instead of group()

    if( matcher.find() ) {
        System.out.println(matcher.group(1));
    }
    

    Anyways, you should not use Regex to parse HTML. Regex is not strong enough to achieve this task. You should probably use some HTML parser like – HTML Cleaner. Also see the link that is provided in one of the comments in the OP. That post is very nice explanation of the problems you can face.

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

Sidebar

Related Questions

need to find an expression for the following problem: String given = { \questionID\
How can i do the following: I have a string something#12 I need a
I'm trying to get a regex which is able to find the following part
I need to get my dates to be into the following format: 12/3/2011 for
I have the following xml that I need to get 2 values (see *
I have the following HTML and I need to get the first div parent
Wondering if I could get some advice and direction on this following requirement: Need
I'm stuck with the following. I'm programming a game and I need to get
I need to do the following and i was wondering if i could get
I have a string in a file a.txt {moslate}alho{/moslate}otra{moslate}a{/moslate} a need to get the

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.