using pattern for xml

Posted on 2004-11-02
Last Modified: 2010-03-31
hi all

i'm a beginner in java..

i want to create a pattern for the following xml:

the content of PartQuantity can be any number (no limitation, but have to be numbers)

currently i use the below codes

String ptn = "(\\p{Punct}PartQuantity\\p{Punct})([\\.0-9])(\\p{Punct}\\/PartQuantity\\p{Punct})";
Pattern pattern = Pattern.compile(ptn);
Matcher matcher = pattern.matcher("<PartQuantity>4000</PartQuantity>");

but the matcher.find() always return me false..

I also have tried some other patterns like:
String ptn = "(<PartQuantity>)([\\.0-9])(<\\p{Punct}+PartQuantity>)";

but still return the same result..

can anybody suggest the correct pattern for the above xml?

thanks :)
Question by:pipinana02
    LVL 3

    Expert Comment


    Seems like you forgot a + after the numbers, and to make it completely decimal:

    Try this:

                    String s = "<PartQuantity>4000</PartQuantity>";
                    Pattern p = Pattern.compile("<PartQuantity>(\\d+\\.?\\d*)</PartQuantity>");

                    Matcher m = p.matcher(s);
                    if (m.matches()) {
                            float f = Float.parseFloat(;
                            System.out.println("f = " + f);

    LVL 3

    Accepted Solution


    If you dont want a floating point, you can change the pattern from


    to just


    and use an

    int i = Integer.parseInt(;

    instead of whats there :)


    Author Comment

    hi aviadbd

    thanks a lot for ur help hehehe :)

    btw can i ask u something:

    when should we use "?" or "*"   ? coz i'm a bit confused on how to differentiate them..

    also what is wrong with  "([\\.0-9])" instead of "(\\d+\\.?\\d*)" ? why the first one cannot work?

    thanks :)
    LVL 3

    Expert Comment


    Well, to know ALL there is to know about patterns, you can check

    About the ? and + specifically:

    If you want to check that an identifier occurs only once if any, you should use ?. + is for once or more. * is for none or more.

    So, checking for [\\.0-9]+ would also acknoledge as a number. However, when you check if a dot occurs only zero or one times (with the ?) and then check for more numbers (the floating point numbers), its more accurate. :)


    Author Comment

    hi aviadbd..

    ou okieee.. thanks a lot for ur help and explanation :)
    LVL 3

    Expert Comment


    No problem :)

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
    Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
    Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
    Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now