PHP 5.3+的可读性较好的正规表达式。
这是PHP移植至 https://github.com/thebinarysearchtree/regexpbuilderjs
RegExpBuilder整合正则表达式至编程语言,从而使它们易于阅读和维护。正则表达式使用链式方法创建。
Examples use Gherkins\RegExpBuilderPHP; $builder = new RegExpBuilder();
Validation $regExp = $builder ->startOfInput() ->exactly(4)->digits() ->then("_") ->exactly(2)->digits() ->then("_") ->min(3)->max(10)->letters() ->then(".") ->anyOf(array("png", "jpg", "gif")) ->endOfInput() ->getRegExp(); //true $regExp->test("2020_10_hund.jpg"); $regExp->test("2030_11_katze.png"); $regExp->test("4000_99_maus.gif"); //false $regExp->test("123_00_nein.gif"); $regExp->test("4000_0_nein.pdf"); $regExp->test("201505_nein.jpg");
Search $regExp = $builder ->multiLine() ->globalMatch() ->min(1)->max(10)->anythingBut(" ") ->anyOf(array(".pdf", ".doc")) ->getRegExp(); $text = <<<EOF Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy SomeFile.pdf eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. doc_04.pdf Stet clita kasd File.doc. EOF; $matches = $regExp->exec($text); //true ($matches[0] === "SomeFile.pdf"); ($matches[1] === "doc_04.pdf"); ($matches[2] === "File.doc");