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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T15:19:30+00:00 2026-05-20T15:19:30+00:00

OK, so I have been searching for hours about my problem but nothing seems

  • 0

OK, so I have been searching for hours about my problem but nothing seems to come up.
So here’s my code snippet followed by the problem:

Pattern forKeys = Pattern.compile("^<feature>\\s*<name>Deviation</name>.*?</feature>", Pattern.DOTALL|Pattern.MULTILINE);
Matcher n = forKeys.matcher("");
String aLine = null;
    while((aLine = in.readLine()) != null) {
         n.reset(aLine);
         String result = n.replaceAll("");
         out.write(result);
         out.newLine();
    }

let’s just assume the undeclared variables are already declared..

my point is, my RegEx (and maybe the matcher also) is not working properly.

I want to erase the parts with the “<feature><name>Deviation</name>*any character/s here*</feature>” included in the ff lines:

<feature>
    <name>Deviation</name>
            <more words here>
</feature>
<feature>
    <name>Average</name>
</feature>
    <feature>
    <name>Deviation</name>
            sample words
</feature>

I think my problem is the use of repititive operators (how to traverse line breaks, tabs, etc), but I can’t seem to find the correct expression.

Any ideas? Thanks in advance.

  • 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-20T15:19:31+00:00Added an answer on May 20, 2026 at 3:19 pm

    Parsing HTML or XML with regex is evil and error-prone.

    Use an XML parser and things will work much better.
    Here’s a solution for your problem using Dom4J:

    // parse XML source
    Document document = DocumentHelper.parseText(yourXmlText);
    
    Iterator<Element> featureIterator =
        // get an iterator for all <feature> elements
        document.getRootElement().elementIterator("feature");
    
    while(featureIterator.hasNext()){
        Element featureElement = featureIterator.next();
        // if <feature> has a child <name> with Content "Deviation"
        if("Deviation").equals(featureElement.elementTextTrim("name")){
            // remove this <feature> element
            featureIterator.remove();
        }
    }
    
    // write modified XML back to file
    new XMLWriter(
        new FileOutputStream(yourXmlFile), OutputFormat.createPrettyPrint()
    ).write(document);
    

    Apart from that you are also making a mistake (see my comments):

    // aLine is just a single line
    while((aLine = in.readLine()) != null) {
         n.reset(aLine);
         // yet you want to replace a multi-line pattern
         String result = n.replaceAll("");
         out.write(result);
         out.newLine();
    }
    

    Your regex might or might not work if you read the entire file to a String, but it can’t work if you apply it on individual lines.

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

Sidebar

Related Questions

I have been searching for a couple hours for a way to accomplish this
I have been searching the web for several hours to try to get an
Hey folks, I have to apologize for my inexperience here, I've been working on
Have been searching for this, I need to have a conditional statement in my
I have been searching for resources to understand thread safety in java and came
I have been searching on how to start an applet from a servlet. Everything
Have been pulling out my hair trying to find out why my sessions are
We have our app all ready to upload to Apple for approval but have
Well, it's been several hours I'm lost... IXSLTemplate::putref_stylesheet doesn't document any error except E_FAIL.
I need to clear sector 0 for removable media devices (custom USB memory devices)

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.