NYCPHP Meetup

NYPHP.org

[nycphp-talk] PCRE expression for tokenizing?

Dan Cech dcech at phpwerx.net
Mon Jul 21 18:51:26 EDT 2008


Dan Cech wrote:
> Michael B Allen wrote:
>> On Mon, Jul 21, 2008 at 6:08 PM, Dan Cech <dcech at phpwerx.net> wrote:
>>> Michael B Allen wrote:
>> So is there any way to say "capture anything that didn't match" (aside
>> from created a sub-expression that explicitly excludes all of the
>> tokens)?
> 
> Afaik no, you could probably do something like:
> 
> preg_match('@^(.*?)(~|\*\*|//|=====|====|===|==|=|$)@',$string,$m);
> 
> Which would give you anything before the first token (or end if there 
> are no more tokens) in $m[1] and the first token (or nothing if there 
> are no more tokens) in $m[2].

This should actually be:

preg_match('@^(.*?)(~|\*\*|//|=====|====|===|==|=|$)@s',$string,$m);

if your string is going to contain newlines.

Dan



More information about the talk mailing list