开发者

regexp freebie ... how do I do this?

What is the correct way to do this?

<?php
$word = 'd开发者_如何学运维ogcatdog';
preg_match( '/do'."[^0-9]".'atdog/i', $word, $matches );
print_r($matches);
?>

I want to return "gc".

I keep getting an error: "Unknown modifier '['".


well if two dogs are surrounding a cat the first thing we can observe is that the cat will start to hiss a lot, this produces a "Hsssss". Then the dogs will start to bark and growl a lot, this gives us a "Grrr Grrr BARK". So together this will produce something like "Hsssss Grrr Grrr Hsssss BARK BARK Hsssss Grrr". So now, remember we want "gc" as our final result we apply the following

$word = 'dogcatdog';
preg_match("[r]+", $word, $matches);
preg_match("(BARK)", $word, $matches);
preg_match("[Hs]+", $word, $matches);
$matches = "c";
preg_match("[G]+", $word, $matchesFinal);
$matches =  $matchesFinal[0] . $matches;

and presto!


preg_match( '/do([a-z]{2})atdog/i', $word, $matches );

Should get it, you would never do [^0-9] if your are trying to find gc. That's just going to find any non-digit character, so would include all punctuation, well anything except 0..9.

Of course, if you really just want to match gc within the string, you'd do.

preg_match( '/do(gc)atdog/i', $word, $matches );

However, that seems fairly pointless.


I don't know specific PHP syntax, but try this:

preg_match( '/do([^0-9]+)atdog/i', $word, $matches );


Try this, i corrected your syntax errors:

<?php
$word = 'dogcatdog';
preg_match("/do([^0-9]+)atdog/i", $word, $matches);
print_r($matches);
?>


(The string syntax in your example was wrong.)

If you want to capture something, you need to enclose it in ( paranthesis ).

preg_match( '/do([^0-9]+)atdog/i', $word, $matches );

In this case gc would appear in $matches[1]. The result array is always enumerated by the capture braces.

And here is a nice list of tools that can help with constructing regular expressions, useful to get started: https://stackoverflow.com/questions/89718/is-there-anything-like-regexbuddy-in-the-open-source-world

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜