Nightmare php preg_match

I just can't get my head around regex, any help is appreciated!

I have a lot of string data that may or may not contain the strings "1/10" or "2/10" or "2/18", etc. In principle, numerators and denominators are subject to change. And to make things more complicated, some input operators can put a space between numerators and denominators !! So my source data could be: "x / y" or "x / y" or "x / y" or "x / y" or "x / y" .... and maybe more combos: (

In any of these cases, I want to determine if x and y are numbers, and there is a “/” between them. Hopelessly in regex, please help

Encoding in php, and I assume that preg_match is what you need to use. Thanks for reading.

+3
source share
4 answers
$pattern = "%(\d+) */ *(\d+)%";

$test = array(
    'half is 1/2',
    '13/100 is your score',
    'only 23 /90 passed',
    'no idea here:7/ 123',
    '24 / 25',
    '1a/2b'
);

foreach($test as $t){
    if(preg_match($pattern, $t, $matches))
        echo "PASS: n:$matches[1], d:$matches[2]\n";
    else
        echo "FAIL: $t\n";
}

outputs:

PASS: n:1, d:2
PASS: n:13, d:100
PASS: n:23, d:90
PASS: n:7, d:123
PASS: n:24, d:25
FAIL: 1a/2b
+4
source
if(preg_match('~^[0-9]+\s*/\s*[0-9]+$~',trim($subject))) {
  // valid
} 
+1
source
if(preg_match('@^\s?[0-9]+\s?\/\s?[0-9]+\s?$@', $s) {
     ...
}
0
source

I think you should avoid regexp at all costs. A few cases you should (could):

  • You are an expert at regexp. If you are you, then you should probably use a regex because it is likely to be faster and shorter. Although readability will come down in my opinion.
  • You need to increase productivity.

Otherwise, my advice is not to use it!

In any of these cases, I want to determine if x and y are numbers, and if there is an “/” between them. Hopelessly in regex, please help

How to solve your problem:

<?php

class Verify {
    public static function numbers($str) {
        $explode = explode("/", $str);

        foreach($explode as $elm) {
            if (!filter_var($elm, FILTER_VALIDATE_INT)) {
                return false;
            }
        }
        return true;
    }
}
class StackTest extends PHPUnit_Framework_TestCase {
    public function testBothPartsAreNumbers() {
        $array = array(
            "1/2",
            "1 / 2",
            "1/ 2",
            "1 /2"
        );
        foreach($array as $elm) {
            $this->assertTrue(Verify::numbers($elm));
        }
    }

    public function testOneOfThemMightBeNotANumber() {
        $array = array(
            "1/a",
            "a/1",
            "1 / a",
            "b/2",
            "b/a",
            "1/2.1",
        );
        foreach($array as $elm) {
            $this->assertFalse(Verify::numbers($elm));
        }
    }
}
?>

alfred@alfred-laptop:~/php/stackoverflow/4916920$ php -v
PHP 5.3.3-1ubuntu9.3 with Suhosin-Patch (cli) (built: Jan 12 2011 16:08:14) 
Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies
with Xdebug v2.1.0, Copyright (c) 2002-2010, by Derick Rethans

# We need at least PHP5.2 for filter.

alfred@alfred-laptop:~/php/stackoverflow/4916920$ phpunit NumberTest.php 
PHPUnit 3.5.10 by Sebastian Bergmann.

..

Time: 0 seconds, Memory: 3.50Mb

OK (2 tests, 10 assertions)
0
source

Source: https://habr.com/ru/post/1790345/


All Articles