SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Hybrid View

  1. #1
    Non-Member
    Join Date
    Oct 2007
    Posts
    363
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)

    Stuck on a regex

    Hey guys,

    I've never been great with regular expressions, and I'm currently stuck on one that some of you may know how to fix.

    I'm trying to do a match for a certain combination of pounds sterling strings.

    I have some unit tests for this, and currently all but a single one are passing.

    Here's the values I'm looking at:

    PHP Code:

    //These values should all fail:
        
    public function provideNonNumericCharacters(){
            return array(
                array(
    '1x'),
                array(
    '100l'),
                array(
    'xxx'),
                array(
    'x1'),
                array(
    '1x.0p'),
                array(
    '1x.0p'),
                array(
    'p'),
                array(
    'xx.50p'),
            );
        }

      
    //These values should all pass:
        
    public function provideCorrectlyFormattedValues(){
            return array(
                array(
    '1'),
                array(
    '18'),
                array(
    '10p'),
                array(
    '10'),
                array(
    '1p'),
                array(
    '100'),
                array(
    '100p'),
                array(
    '1.1'),
                array(
    '1.10p'),
                array(
    '1.56'),
                array(
    '1.56p'),
                array(
    '1p')
            );
        } 
    The only unit test out of all of these that is failing is for the value: 'p'. This value should be blocked, but it passes.

    Here is the current expression I'm using: "/^(\p{Sc}?\d*(p{1})?)(?:\.\d{0,}p?)?$/u"

    Any ideas?

    Cheers!

  2. #2
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,050
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Would you want \d+ instead of \d* denoting 1 or more instead of 0 or more?
    Be sure to congratulate Patche on earning July's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  3. #3
    Non-Member
    Join Date
    Oct 2007
    Posts
    363
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cpradio View Post
    Would you want \d+ instead of \d* denoting 1 or more instead of 0 or more?
    nfiguration read from /vagrant/app/phpunit.xml.dist

    ............................................

    Time: 35.39 seconds, Memory: 12.75Mb

    OK (44 tests, 46 assertions)


    Cpradio - I owe you a drink

  4. #4
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,050
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    One other test you may want to add is '.p' (notice the dot), I think that may pass when it should fail and the correction would be to change \d{0,} to \d{1,} If it fails like it should, then no harm done, but I do really think that will pass too

    Edit:

    Actually, the more I think about it, it will likely fail due to the \d+ change, but it would have passed prior to that. You may want to also try '0.p', which would pass the \d+ change and should fail the \d{1,} test. Of course, '0.p' may be considered valid for your program.
    Last edited by cpradio; Aug 20, 2013 at 19:47.
    Be sure to congratulate Patche on earning July's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  5. #5
    Non-Member
    Join Date
    Oct 2007
    Posts
    363
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cpradio View Post
    One other test you may want to add is '.p' (notice the dot), I think that may pass when it should fail and the correction would be to change \d{0,} to \d{1,} If it fails like it should, then no harm done, but I do really think that will pass too

    Edit:

    Actually, the more I think about it, it will likely fail due to the \d+ change, but it would have passed prior to that. You may want to also try '0.p', which would pass the \d+ change and should fail the \d{1,} test. Of course, '0.p' may be considered valid for your program.
    Yeah, all good: '.p' fails, and '0.p' passes. It's fine for '0.p' to pass.

    Cheers!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •