regexpbuilderphp
2.1.1
____ _____ ____ _ _ _ ____ _ _ ____
| _ ___ __ _| ____|_ ___ __ | __ ) _ _(_) | __| | ___ _ __| _ | | | | _
| |_) / _ / _` | _| / / '_ | _ | | | | | |/ _` |/ _ '__| |_) | |_| | |_) |
| _ < __/ (_| | |___ > <| |_) | |_) | |_| | | | (_| | __/ | | __/| _ | __/
|_| ____|__, |_____/_/_ .__/|____/ __,_|_|_|__,_|___|_| |_| |_| |_|_|
|___/ |_|
regexpbuilderjs
의 PHP 포트
RegExpBuilder는 정규식을 프로그래밍 언어에 통합하므로 정규식을 쉽게 읽고 유지 관리할 수 있습니다. 정규식은 배열이나 문자열과 같은 연결된 메서드와 변수를 사용하여 생성됩니다.
composer req gherkins/regexpbuilderphp
또는 적절한 릴리스를 다운로드하고 /src
에서 RegExpBuilder.php
및 RegExp.php
요구하십시오.
https://github.com/gherkins/regexpbuilderphp/wiki
use Gherkins RegExpBuilderPHP ;
$ builder = new RegExpBuilder ();
$ 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 -> matches ( " 2020_10_hund.jpg " );
$ regExp -> matches ( " 2030_11_katze.png " );
$ regExp -> matches ( " 4000_99_maus.gif " );
//false
$ regExp -> matches ( " 123_00_nein.gif " );
$ regExp -> matches ( " 4000_0_nein.pdf " );
$ regExp -> matches ( " 201505_nein.jpg " );
$ regExp = $ builder
-> multiLine ()
-> globalMatch ()
-> min ( 1 )-> max ( 10 )-> anythingBut ( " " )
-> anyOf ( array ( " .pdf " , " .doc " ))
-> getRegExp ();
$ text = ' 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. '
$ matches = $ regExp -> findIn ( $ text );
//true
( $ matches [ 0 ] === " SomeFile.pdf " );
( $ matches [ 1 ] === " doc_04.pdf " );
( $ matches [ 2 ] === " File.doc " );
$ regExp = $ builder
-> min ( 1 )
-> max ( 10 )
-> digits ()
-> getRegExp ();
$ text = " 98 bottles of beer on the wall " ;
$ text = $ regExp -> replace (
$ text ,
function ( $ match ) {
return ( int ) $ match + 1 ;
}
);
//true
( " 99 bottles of beer on the wall " === $ text );
$ a = $ builder
-> startOfInput ()
-> exactly ( 3 )-> digits ()
-> anyOf ( array ( " .pdf " , " .doc " ))
-> endOfInput ();
$ b = $ builder
-> getNew ()
-> startOfInput ()
-> exactly ( 4 )-> letters ()
-> then ( " .jpg " )
-> endOfInput ();
$ regExp = $ builder
-> getNew ()
-> eitherFind ( $ a )
-> orFind ( $ b )
-> getRegExp ();
//true
$ regExp -> matches ( " 123.pdf " );
$ regExp -> matches ( " 456.doc " );
$ regExp -> matches ( " bbbb.jpg " );
$ regExp -> matches ( " aaaa.jpg " );
//false
$ regExp -> matches ( " 1234.pdf " );
$ regExp -> matches ( " 123.gif " );
$ regExp -> matches ( " aaaaa.jpg " );
$ regExp -> matches ( " 456.docx " );
더 많은 예제를 보려면 테스트를 살펴보세요.