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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:29:53+00:00 2026-05-16T05:29:53+00:00

I’m making an XMLParser for a Java program (I know there are good XMLParsers

  • 0

I’m making an XMLParser for a Java program (I know there are good XMLParsers out there but I just want to do it).

I have a method called getAttributeValue (String xmlElement, String attribute) and am using regex to find a sequence of characters that have the attribute name plus

="any characters that aren't a double quote"

I can then parse the contents of the quotes. Unfortunately, I’m having trouble with the regex pattern. If I use:

Pattern p = Pattern.compile(attribute + "=\"(.)+\"");

Then I get a string starting with my attribute name, but because there are loads of attributes and values and the last one’s value has the double quotes, I get the string I want plus all the other attribute names and values like so:

attributeOne="contents" attributeTwo="contents2" attributeThree="contents3"

So I thought that I could have a regex pattern that, instead of the "." Any characters symbol, would have "any characters but not a double quote". I have tried:

Pattern p = Pattern.compile(attribute + "=\"(.&&[^\"])+\"");
Pattern p = Pattern.compile(attribute + "=\"(.&&(^\"))+\"");
Pattern p = Pattern.compile(attribute + "=\"([.&&[^\"]]+)\"");

But none of them work. I’d be grateful for any suggestions and comments.

  • 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-16T05:29:54+00:00Added an answer on May 16, 2026 at 5:29 am

    The regular expression pattern for:

    ="any characters that aren't a double quote"
    

    Is ="[^"]*", which as a Java string literal is "=\"[^\"]*\"".

    The [...] construct is called a character class; e.g. [aeiou] matches one of any of the lowercase vowels. The [^...] construct is a negated character class; e.g. [^aeiou] matches one of anything but the lowercase vowels (which includes consonants, symbols, digits, etc).

    Note that this pattern does not allow escaped " in the String (see link below for patterns that account for this possibility).

    References

    • regular-expressions.info/Character Class and Pattern Examples of Programming Languages

    Related questions

    • Regex: why doesn’t [01-12] range work as expected?
      • detailed discussion about character class, common beginner pitfalls, etc

    On greedy, reluctant, and negated character class matching

    To understand why ".+" doesn’t “work” as expected, and why sometimes you see ".+?" reluctant version to try to “fix” this problem, consider the following example:

    Example 1: From A to Z

    Let’s compare these two patterns: A.*Z and A.*?Z.

    Given the following input:

    eeeAiiZuuuuAoooZeeee
    

    The patterns yield the following matches:

    • A.*Z yields 1 match: AiiZuuuuAoooZ (see on rubular.com)
    • A.*?Z yields 2 matches: AiiZ and AoooZ (see on rubular.com)

    Let’s first focus on what A.*Z does. When it matched the first A, the .*, being greedy, first tries to match as many . as possible.

    eeeAiiZuuuuAoooZeeee
       \_______________/
        A.* matched, Z can't match
    

    Since the Z doesn’t match, the engine backtracks, and .* must then match one fewer .:

    eeeAiiZuuuuAoooZeeee
       \______________/
        A.* matched, Z still can't match
    

    This happens a few more times, until finally we come to this:

    eeeAiiZuuuuAoooZeeee
       \__________/
        A.* matched, Z can now match
    

    Now Z can match, so the overall pattern matches:

    eeeAiiZuuuuAoooZeeee
       \___________/
        A.*Z matched
    

    By contrast, the reluctant repetition in A.*?Z first matches as few . as possible, and then taking more . as necessary. This explains why it finds two matches in the input.

    Here’s a visual representation of what the two patterns matched:

    eeeAiiZuuuuAoooZeeee
       \__/r   \___/r      r = reluctant
        \____g____/        g = greedy
    

    Example: An alternative

    In many applications, the two matches in the above input is what is desired, thus a reluctant .*? is used instead of the greedy .* to prevent overmatching. For this particular pattern, however, there is a better alternative, using negated character class.

    The pattern A[^Z]*Z also finds the same two matches as the A.*?Z pattern for the above input (as seen on ideone.com). [^Z] is what is called a negated character class: it matches anything but Z.

    The main difference between the two patterns is in performance: being more strict, the negated character class can only match one way for a given input. It doesn’t matter if you use greedy or reluctant modifier for this pattern. In fact, in some flavors, you can do even better and use what is called possessive quantifier, which doesn’t backtrack at all.

    References

    • regular-expressions.info/Repetition – An Alternative to Laziness, Negated Character Classes and Possessive Quantifiers

    Example 2: From A to ZZ

    This example should be illustrative: it shows how the greedy, reluctant, and negated character class patterns match differently given the same input.

    eeAiiZooAuuZZeeeZZfff
    

    These are the matches for the above input:

    • A[^Z]*ZZ yields 1 match: AuuZZ (as seen on ideone.com)
    • A.*?ZZ yields 1 match: AiiZooAuuZZ (as seen on ideone.com)
    • A.*ZZ yields 1 match: AiiZooAuuZZeeeZZ (as seen on ideone.com)

    Here’s a visual representation of what they matched:

             ___n
            /   \              n = negated character class
    eeAiiZooAuuZZeeeZZfff      r = reluctant
      \_________/r   /         g = greedy
       \____________/g
    

    Related questions

    • Difference between .? and . for regex
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 540k
  • Answers 539k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer There's a permission issue with your .gem folder. Make sure… May 17, 2026 at 2:28 am
  • Editorial Team
    Editorial Team added an answer Google does find a lot of info: http://www.drewnoakes.com/snippets/GdiColorChart/ http://www.pardesiservices.com/softomatix/reflectionhatch.asp http://www.codeproject.com/KB/combobox/HatchStyleComboBox.aspx… May 17, 2026 at 2:27 am
  • Editorial Team
    Editorial Team added an answer From the question I understand you are too lazy to… May 17, 2026 at 2:27 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have a French site that I want to parse, but am running into
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

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.