• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 298
  • Last Modified:

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 :)
0
pipinana02
Asked:
pipinana02
  • 4
  • 2
1 Solution
 
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
 
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now