简体   繁体   中英

Regular expression numbers and hyphens

I have a function that searches a string for a letter and/or number combination which works well, except sometimes that combination also contains a hyphen which I would like to match too. The regex below is taken from within a larger regex pattern match, but this is the part of the expression that i am stuck on.

For example:

([A-Za-z0-9-]+)

Matches the string 123 or 123A but it does not match 123-125 or 123A-125A

I'm sure it is a simple solution but it's not my forte.

thanks in advance

$key= preg_replace("/http:\/\/www.website.com\/[0-9]+\/([A-Za-z0-9-]+)_([A-Za-z_]+)_(MUSIC|VIDEO|PHOTOS)_([A-Z_]+)_(..+)_([0-9]+)/i", '${1}, ${2}, ${3}, ${4}, ${5}, ${6}', $url);

很简单,您需要在字符集中转义连字符:

([A-Za-z0-9\-]+)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM