Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

XML pattern matching for numbers

Last post 05-14-2008, 7:15 PM by Aussie Susan. 16 replies.
Page 1 of 2 (17 items)   1 2 Next >
Sort Posts: Previous Next
  •  05-13-2008, 10:25 AM 42178

    XML pattern matching for numbers

    I am writing an XML schema for my company and I am having a problem with a RegEx pattern.  I have to support the following values in the XML:

    • 500
    • 500.00 (yes, the 2 ending 0's must be kept and can not be shortened to 500)
    • 0.000
    • 0
    • .000
    • -.123
    • +500
    • +.123

    What I do not want is "blanks", commas, alphabetic characters.  This is basically a decimal field represented by a String type (because of the requirement of keeping the ending 0's).  This will be used by XML validators (i.e. xsd:pattern).  I believe it matches up to a Java RegEx parser (not 100% sure).

     
    I have found very useful RegEx's in the library but none that will allow both .123 and not allow a blank.  Some of those RegEx's are:

    • ^-?\d*(\.\d+)?$  (will allow .123 but also allows blanks)
    • ^[-+]?\d+(\.\d+)?$ (will not allow blanks, but also does not allow .123

    Thanks for your help.



     

  •  05-13-2008, 10:56 AM 42180 in reply to 42178

    Re: XML pattern matching for numbers

    I have been doing some more testing and I believe the following RegEx will accomplish my goal stated above:

    ^[-+]?(\.\d+)?\d+(\.\d+)?$

     

    Your thoughts?  Am I missing anything?  I sometimes have issues figuring out all the possible combination of tests...

     

  •  05-13-2008, 11:06 AM 42183 in reply to 42180

    Re: XML pattern matching for numbers

    yr regex will match on

    +001

    is it ok?

     

  •  05-13-2008, 11:12 AM 42184 in reply to 42183

    Re: XML pattern matching for numbers

    u can build upon this regex that partially does what u want:

    ^(?!0\d|[+-]0$)[+-]?\d+(\.00)?$

    it will not allow inputs like

    -0

    +0

    0001

    234.000

     

  •  05-13-2008, 1:09 PM 42192 in reply to 42183

    Re: XML pattern matching for numbers

    Yes, matching on +001 is fine.  I have to keep whatever the vendor is passing to us - no changes, but I want to make sure it is a number.
  •  05-13-2008, 1:15 PM 42193 in reply to 42184

    Re: XML pattern matching for numbers

    I actually need it to match on all those:

    -0, +0, 00001, 234.000

     

    The system my company is building will take QA test data for chemicals from a Lab and store it in our MRP system.  Here is where things get crazy.  The numbers 500, 500.0, 500.00, 500.000 are all the same value - but actually different test results (i.e.  500 <> 500.0).  There are reasons for this logic (has to do with limitations in the MRP system).  This is why I can not use the xsd:decimal type and must use xsd:string.  Since I am using xsd:string, I now need to make sure they are sending me a valid number (while keeping all the extra 0's).

     

    Thanks for all your help.  Do you see any issues with these requirements and my RegEx that I created above?

     

  •  05-13-2008, 2:04 PM 42199 in reply to 42193

    Re: XML pattern matching for numbers

    i'll get back to you after work

  •  05-13-2008, 7:04 PM 42217 in reply to 42199

    Re: XML pattern matching for numbers

    try this one:

    ^[+-]?\d+(\.0(0|00))?$|^[+-]?\.0(0|00)$

    matches pretty much all the inputs u mentioned.

    how about this inout

    00.000

    still valid?

  •  05-13-2008, 8:29 PM 42219 in reply to 42217

    Re: XML pattern matching for numbers

    Yes, I personally don't consider it valid, but it is...   I am relatively new to RegEx (rarely use it) so I have trouble reading it.  Can you explain the differences between your RegEx expression and the one I posted:

                 ^[-+]?(\.\d+)?\d+(\.\d+)?$

     A value like .000 or .12 is also valid.  Does your RegEx allow that?

     

    Thanks for all your help. 

     

  •  05-13-2008, 9:04 PM 42223 in reply to 42219

    Re: XML pattern matching for numbers

    Personally, given the fact that you want to accept whatever the format of the value as long as it is numeric, then I would use:

    [+-]?(\d+|(?=\.))(.\d+)?

    This matches all of the examples you have provided.

    A problem with your pattern is that it also matches ".12.345" which fits your criteria but is probably not what you want(could be confused with an account number etc.).

    Susan 

  •  05-13-2008, 11:18 PM 42225 in reply to 42223

    Re: XML pattern matching for numbers

    Aussie Susan:

    Personally, given the fact that you want to accept whatever the format of the value as long as it is numeric, then I would use:

    [+-]?(\d+|(?=\.))(.\d+)?

    This matches all of the examples you have provided.

    A problem with your pattern is that it also matches ".12.345" which fits your criteria but is probably not what you want(could be confused with an account number etc.).

    Susan 

     

    Xml Schema regexes do not support lookarounds. 


    Michael

    "In theory, theory and practice are the same. In practice, they are not."
    Albert Einstein
  •  05-13-2008, 11:38 PM 42227 in reply to 42225

    Re: XML pattern matching for numbers

    Fair enough, then how about:

    ^[+-]?(\d+(\.\d+)?|\.\d+)$

    which also corrects a small mistake in my previous suggestion (the second dot should have been escaped to make it a literal) and includes the ^ and $ placeholders.

    Susan 

  •  05-13-2008, 11:48 PM 42228 in reply to 42193

    Re: XML pattern matching for numbers

    bkschroll:

    I actually need it to match on all those:

    -0, +0, 00001, 234.000

     

    The system my company is building will take QA test data for chemicals from a Lab and store it in our MRP system.  Here is where things get crazy.  The numbers 500, 500.0, 500.00, 500.000 are all the same value - but actually different test results (i.e.  500 <> 500.0).  There are reasons for this logic (has to do with limitations in the MRP system).  This is why I can not use the xsd:decimal type and must use xsd:string.  Since I am using xsd:string, I now need to make sure they are sending me a valid number (while keeping all the extra 0's).

     

    Thanks for all your help.  Do you see any issues with these requirements and my RegEx that I created above?

     

    I'm not quite clear on why you can't use xsd:double in your schema. Are you processing the value as numbers or strings.  For the sake of validation I don't see the why you'd need a string.

    Also even it the type was numeric you could still up a regex to restrict the format. 


    Michael

    "In theory, theory and practice are the same. In practice, they are not."
    Albert Einstein
  •  05-13-2008, 11:52 PM 42229 in reply to 42227

    Re: XML pattern matching for numbers

    Aussie Susan:

    Fair enough, then how about:

    ^[+-]?(\d+(\.\d+)?|\.\d+)$

    which also corrects a small mistake in my previous suggestion (the second dot should have been escaped to make it a literal) and includes the ^ and $ placeholders.

    Susan 

    You can drop the boundary placeholders, they are implied.  XML Schema regexes are all (of the input) or nothing matches. 

    The boundary placeholder aren't supported either. 


    Michael

    "In theory, theory and practice are the same. In practice, they are not."
    Albert Einstein
  •  05-14-2008, 3:01 AM 42233 in reply to 42229

    Re: XML pattern matching for numbers

    The problem is that, without the boundary placeholders, it will match the start of "34.56.78" etc.. Sure it will pick out a valid number, but there needs to be something that forces it go try to go to the end of the text.

    Susan 

Page 1 of 2 (17 items)   1 2 Next >
View as RSS news feed in XML