Re: Regular expression to find from start of string to first space
| From: | David Tulloh | Date: | Tue, 08 Aug 2006 09:42:33 +0000 |
| Subject: | Re: Regular expression to find from start of string to first space | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-240302@lists.php.net to get a copy of this message | ||
Dave M G wrote:
> PHP,
>
> Shouldn't this regular expression select everything from the start of
> the string to the first space character:
>
> $firstWord = preg_match('#^*(.*) #iU', $word);
>
> It doesn't, so clearly I'm wrong, but here's why I thought it would:
>
> The enclosing has marks, "#", I *think* just encloses the expression. I
> was told to use them before, but I can't find them here:
> http://jp2.php.net/manual/en/reference.pcre.pattern.syntax.php
>
> The caret, "^", says to start at the beginning of the line.
>
> The first asterix, "*" after the caret says to use any starting character.
Here's where you go wrong. The * means match the previous character 0
or more times. In this case I'm actually not sure what it would do.
Given that you grab everything with the (.*) the first * is not needed
at all.
David