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:



//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!

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 :slight_smile:

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 :slight_smile:

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!