Web lists-archives.com

Re: [PHP] RegOops

> On Oct 23, 2015, at 05:44, Karl DeSaulniers <karl@xxxxxxxxxxxxxxx> wrote:
> With the given string..
> vehicle10-vehicle-name
> Running regex in a preg_match like
> "/(\w+)([0-9+]+)-(.*)/"
> I am getting.
> array(
> 	0	=>	vehicle10-vehicle-name
> 	1	=>	vehicle1
> 	2	=>	0
> 	3	=>	vehicle-name
> )

The \w sequence does match numbers — think “word” characters as in legal characters in variable names (though it’s actually derived from Perl, not PHP). The more curious question is why are the 1 and the 0 being split. The reason for that is because the first part of your expression is fighting with the second part: they’re both greedy, so the first part grabs everything through 0, then has to be backed off one character so that the second part can grab at least one digit in order to satisfy the overall expression.

Also, the ‘+’ after the 9 in the second part is unnecessary, at least based on this sample data. You’d only need it if you specifically want to include a ‘+’ character in the number sequence, but I suspect you intended it as a repetition operator. Putting it in the brackets, however, it’s taken literally.

Bob Williams
Business Unit Information Officer and
Senior Vice President of Software Development
Newtek Business Services Corp.

Attachment: signature.asc
Description: Message signed with OpenPGP using GPGMail