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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:15:07+00:00 2026-06-17T09:15:07+00:00

Input : <tag>Testing different formatting options in </tag><tag class=classA classB>Text</tag><tag class=classC>Class C text</tag> Expected

  • 0

Input :

<tag>Testing different formatting options in </tag><tag class="classA classB">Text</tag><tag class="classC">Class C text</tag>

Expected Output :

<tag>Testing different formatting options in </tag><tagA><tabB>Text</tagA></tagB><tagC>Class C text</tag>

Basically the tag is replaced by tags based on the attributes in “class”. ie., if the attributes has a classA attribute then the tag will be replaced by tagA, if classB attribute is also present then the tag will also include tagB and so on..

Attempt made :

    final String TAG_GROUPS = "<tag class=\"(.*)\">(.*)</tag>";
    Pattern pattern = Pattern.compile(TAG_GROUPS);
    Matcher matcher = pattern.matcher(inputString);

The output I am getting fails to find the matching tags. In particular the statement

    String classes = matcher.group(1);

gives the string classA classB">Text</tag><tag class="classC">Class C text</tag. The pattern matcher is failing to find the matching tags. I am a beginner to regular expressions and I would like to know the right pattern for the problem. Any help is appreciated.

  • 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-17T09:15:08+00:00Added an answer on June 17, 2026 at 9:15 am

    When you use * it will try to absorb all possible characters (greedy).

    If you want that .* to match the less possible characters you must use lazy match with *?.

    So your regex get as:

    <tag class=\"(.*?)\">(.*?)</tag>
    

    Above, is the easy way. But isn’t necessary the optimum way. Lazy match is more slow than greedy and if you can, you must try to avoid it. For example if you estimate that you code will be correct (not tag broken without a close tag, etc) is better that you use negate classes instead of .*?. For example, you regex can be write as:

    <tag class="([^"]*)">([^<]*)</tag>
    

    Witch is more efficient for the regex engine (although is not always possible to convert lazy match to negate class).

    And of course, if you are trying to parse a complete html or xml document in witch you must do many different changes, it’s better to use a xml (html) parser.

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

Sidebar

Related Questions

Pattern - <(\w+)>.+</\1> Input String - Testing <tag>some <b>idiot</b> text</tag>idiot text continue.. Now I
I have the following input tag: <input type=text id=title class=title maxlength=20> I need day
i have this tag as input tag: <a href=controller.jsp?sid=127490C88DB5&R=35144 class=11-link-dkred-bold><b>Mr. John Q. Anderson&nbsp;&nbsp;&nbsp;MBA 1977
I developed an app in phonegap . In input tag if i give type='text'
I am not sure if Select Tag can work with Hidden Text Input like
<!DOCTYPE html> <html> <head> <meta charset=utf-8> <title>Javascript Form Testing</title> </head> <body> Name: <input type=text
i have an input tag that i use to search for text in the
I have got a simple input tag <input type=text id=textb name=textbox/> jquery is $(document).ready(function(){
How to validate using Javascript an input tag of type text in an html
How to work jquery autocomplete with select tag ? HTML <input type=text id=search onkeydown=autoCompleteByTag(this)

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.