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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:43:48+00:00 2026-06-13T05:43:48+00:00

Sorry guys, Ive googled and still cant get my code to work. Not exactly

  • 0

Sorry guys, Ive googled and still cant get my code to work. Not exactly a whiz with java (yet, but give me time 🙂 ). I have an xml document that i am using a DOM parser to read, extract the class attributes and now i need to exclude some of those attributes using regex. For instance, my output so far is:

[[#text: ns1:Spare3]]

[[#text: ns1:Spare4]]

[[#text: ns1:Spare5]]

[[#text: ns1:Street]]

[[#text: ns1:Anything]]

[[#text: ns1:TearLineDateUpdated]]

[[#text: ns1:SourceReportTearline]]

[[#text: ns1:AnyFilter]]

[[#text: ns1:UpdatedByTelecom]]

[[#text: ns1:UpdatedByName]]

and i need to exclude those lines that contain the words Spare, or start with TearLine (not case sensitive) and a few others.

My code snippet (that i wrote to test with) says:

Pattern p = Pattern.compile(".*?\\Spare\\(.*?\\)",    
Pattern.CASE_INSENSITIVE|Pattern.DOTALL | Pattern.MULTILINE);
Matcher m = p.matcher((nl.item(i)).toString());
if (m.matches())
{
System.out.println("["+nl.item(i)+"]" + "matched"); 
}
else
{
System.out.println("["+nl.item(i)+"]" + "not matched");     
}

How do i exclude any lines that contain the word Spare and any lines that start with TearLine (but TearLine can occur elsewhere in the word and thats ok).?

  • 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-13T05:43:49+00:00Added an answer on June 13, 2026 at 5:43 am

    Are those the actual strings you’re trying to match? That is, the DOM parser produced those strings, and now you’re applying the regex to them? If so, you want something like this:

    Pattern p = Pattern.compile(
        "ns1:(tearline|.*spare)", Pattern.CASE_INSENSITIVE
    );
    Matcher m = p.matcher("");
    
    String[] inputs = {
        "[[#text: ns1:Spare3]]",
        "[[#text: ns1:Spare4]]",
        "[[#text: ns1:Spare5]]",
        "[[#text: ns1:Street]]",
        "[[#text: ns1:Anything]]",
        "[[#text: ns1:TearLineDateUpdated]]",
        "[[#text: ns1:SourceReportTearline]]",
        "[[#text: ns1:AnyFilter]]",
        "[[#text: ns1:UpdatedByTelecom]]",
        "[[#text: ns1:UpdatedByName]]"
    };
    
    for (String s : inputs)
    {
      System.out.printf( "%n%5b => %s%n", !m.reset(s).find(), s );
    }
    

    output:

    false => [[#text: ns1:Spare3]]
    
    false => [[#text: ns1:Spare4]]
    
    false => [[#text: ns1:Spare5]]
    
     true => [[#text: ns1:Street]]
    
     true => [[#text: ns1:Anything]]
    
    false => [[#text: ns1:TearLineDateUpdated]]
    
     true => [[#text: ns1:SourceReportTearline]]
    
     true => [[#text: ns1:AnyFilter]]
    
     true => [[#text: ns1:UpdatedByTelecom]]
    
     true => [[#text: ns1:UpdatedByName]]
    

    Notes:

    • I used find() instead of matches() so my regex only has to match the part that interests me, not the whole string.

    • Some of the other responders used ^TearLine because you said that word had to appear at the beginning of the line, but if my guess is right, you really want to match it right after the ns1: prefix. On the other hand, .*spare allows spare to appear anywhere, not just at the beginning (.*?spare works, too).

    • Similarly, Ωmega used "\\bSpare\\b" on the assumption that you were interested only in the complete word Spare. I left out the word boundaries (\b) because you seem to want to match things like Spare3 or (I’m guessing) FooSpare.

    • I don’t know why you added \\(.*?\\) to your regex, since there were no parentheses in your sample strings.

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

Sidebar

Related Questions

guys sorry for being so noob but im still starting yet with my php,
Sorry guys, I can't post images yet, so if it's not too much trouble,
Hey guys, sorry for this probably fairly simple question but I'm starting to get
Sorry guys, didn't know how to word this one. Still learning jquery and have
Sorry guys, not being lazy, I know others have had the same error message
Sorry guys! i am so into the code! that i forgot to put the
I could be not more sorry guys - the question was a very wrong
Sorry for the wall of text guys but this one requires expliaining, way too
Sorry guys, I'm a noob. I know that some languages support type casting, but
Sorry guys, I hate asking dumb questions but I have seriously been searching for

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.