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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:32:25+00:00 2026-05-23T13:32:25+00:00

I have a file with some custom tags and I’d like to write a

  • 0

I have a file with some custom tags and I’d like to write a regular expression to extract the string between the tags. For example if my tag is:

[customtag]String I want to extract[/customtag]

How would I write a regular expression to extract only the string between the tags. This code seems like a step in the right direction:

Pattern p = Pattern.compile("[customtag](.+?)[/customtag]");
Matcher m = p.matcher("[customtag]String I want to extract[/customtag]");

Not sure what to do next. Any ideas? Thanks.

  • 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-05-23T13:32:25+00:00Added an answer on May 23, 2026 at 1:32 pm

    You’re on the right track. Now you just need to extract the desired group, as follows:

    final Pattern pattern = Pattern.compile("<tag>(.+?)</tag>", Pattern.DOTALL);
    final Matcher matcher = pattern.matcher("<tag>String I want to extract</tag>");
    matcher.find();
    System.out.println(matcher.group(1)); // Prints String I want to extract
    

    If you want to extract multiple hits, try this:

    public static void main(String[] args) {
        final String str = "<tag>apple</tag><b>hello</b><tag>orange</tag><tag>pear</tag>";
        System.out.println(Arrays.toString(getTagValues(str).toArray())); // Prints [apple, orange, pear]
    }
    
    private static final Pattern TAG_REGEX = Pattern.compile("<tag>(.+?)</tag>", Pattern.DOTALL);
    
    private static List<String> getTagValues(final String str) {
        final List<String> tagValues = new ArrayList<String>();
        final Matcher matcher = TAG_REGEX.matcher(str);
        while (matcher.find()) {
            tagValues.add(matcher.group(1));
        }
        return tagValues;
    }
    

    However, I agree that regular expressions are not the best answer here. I’d use XPath to find elements I’m interested in. See The Java XPath API for more info.

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

Sidebar

Related Questions

I have text file with some stuff that i would like to put into
I have some values in a configuration file (XML file) with some values like
I have a web control with some custom javascript. The javascript creates new file
I'd like to make some custom MenuHeaders in WPF so I can have (for
I have set up some custom symfony project-level settings in the file config/project.yml following
I want to have some custom configuration (read in from file) available throughout my
I have created some custom PowerShell Cmdlets in C# and would like to provide
i have some file test.php <?PHP $config_key_security = test; ?> and i have some
I have text file with some text information and i need to split this
I did it before... But I forgot. I have a file with some data:

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.