selvinortiz / flux Goto Github PK
View Code? Open in Web Editor NEWFluent regular expressions in PHP
License: MIT License
Fluent regular expressions in PHP
License: MIT License
I think you should really credit this source properly, since this is apparently a port:
https://github.com/jehna/VerbalExpressions
Note that there's already a PHP port in that repo, although this one is much better :) And also there's this port:
https://github.com/markwilson/VerbalExpressionsPhp
Maybe you guys should get together and work on one definitive PHP version?
Hi,
Thanks for lib, great. Slight issue
->letters(0,1)
creates a pattern
[a-zA-Z]+
rather than
[a-zA-Z]{0,1}
Fix is to compare to null rather than if ($min) which fails if ZERO provided.
Same in all other functions where min and max allowed.
Matt
public function letters($min = null, $max = null)
{
if (!is_null($min) && !is_null($max))
{
return $this->raw(sprintf('([a-zA-Z]{%d,%d})', $min, $max));
}
elseif (!is_null($min) && is_null($max))
{
return $this->raw(sprintf('([a-zA-Z]{%d})', $min));
}
else
{
return $this->raw('([a-zA-Z]+)');
}
}
the implementation of:
$flux->not('something');
would be of great use to myself
(?!something)
An implementation of:
public function not($string)
{
return $this->raw(sprintf('(?!%s)', $string));
}
I don't know if my port has done something wrong here, but the example of US Date doesn't match my output.
// Pattern /^(\()(\d{3})(\))( )?(\d{3})([ \-])(\d{4})$/
echo $flux->match( $date ) ? 'matched' : 'unmatched'; // matched
echo $flux->replace( '$3/$5/$7', $date ); // 612.424.0013
Yet mine outputs:
//Pattern (\w+)(, )([a-zA-Z]{3})( )(\d{1,2})(, )(\d{4})
// matched
// Jul/22/2013
And here is the code:
var USDate = new Flux();
var testDate = 'Monday, Jul 22, 2013';
USDate.startOfLine().word().then(', ').letters(3).then(' ').digits(1, 2).then(', ').digits(4).endOfLine();
console.log(USDate.pattern.join(''));
console.log(USDate.match(testDate) ? "matched" : "unmatched");
console.log(USDate.replace('$3/$5/$7', testDate));
Anything wrong there?
I haven't tested your code as I don't have PHP setup here.
Since PHP 7.1
version is released and it's current PHP version.
I think this should be considered.
Once this issue is accepted, I would be happy to this :).
What does the seed do? It's default is false
but there is no way of overwriting it. It's not changed anywhere, so is it needed?
Except this it's a great lib!
@selvinortiz, I've written a Node.js port of flux which you can install from the NPM registry here. Function names are the same, it's almost a straight port, only a few changes for using new RegExp
, the tests pass though.
I'll try and keep the port up to date with this repository and update the README later.
Is there currently functionality for this type of regular expression:
([A-Z]+)?
Something like:
$flux->maybe($flux->letters());
or
$flux->maybeLetters();
Any help in this regard would be greatly appreciated
It feels like based on the fluent interface we're building it would be a good idea to implement common patterns to make the API more fluent.
Maybe methods such as:
Thoughts?
Since you can getSegment
, would it be worth being able to removeSegment
? It'd only be used really when running tests or maybe changing your regex based on a later condition.
I would like to find 0 or more digits, like so:
$flux->digits(0, 10);
however, the digits method will always fall to the else, as 0 is false and the condition states $min && $max see below:
if ($min && $max)
{
return $this->raw(sprintf('(\\d{%d,%d})', $min, $max));
}
I may be misunderstanding how to achieve the 0 or more with the Flux library. However
if (! is_null($min) && ! is_null($max))
Would resolve the issue for my use case. What would you thoughts be on this?
There should be a method for wrapping words in \bword\b
boundaries boundary
and perhaps a beginBoundary
and endBoundary
method too.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.