using pattern for xml

hi all

i'm a beginner in java..

i want to create a pattern for the following xml:
<PartQuantity>4000</PartQuantity>

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 :)
pipinana02Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

aviadbdCommented:
Pipinana,

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(m.group(1));
                        System.out.println("f = " + f);
                }


Aviad.
0
aviadbdCommented:

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

<PartQuantity>(\\d+\\.?\\d*)</PartQuantity>

to just

<PartQuantity>(\\d+)</PartQuantity>

and use an

int i = Integer.parseInt(m.group(1));

instead of whats there :)

Aviad.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
pipinana02Author Commented:
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 :)
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

aviadbdCommented:

Well, to know ALL there is to know about patterns, you can check http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.html

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 9.92.123.3 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. :)

Aviad.
0
pipinana02Author Commented:
hi aviadbd..

ou okieee.. thanks a lot for ur help and explanation :)
0
aviadbdCommented:

No problem :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.