我需要匹配图像文件的部分文件名。 我有两种类型:
img000-size.jpg img000.jpg
我怎么才能匹配img000?
PS:我能用preg_match获取php代码吗? 感谢
答案 0 :(得分:0)
好的,让我帮你......
正则表达式:/(\w+)(?:-\w+)?\.jpg/i
数据:477547755-large.jpg
匹配:477547755
我匹配1和无限次之间的单词字符,而不是可选的减号,再单词字符,然后是扩展名。 注意:您必须在扩展名处转义点,因为它将被解释为“任何字符”
描述:
1st Capturing group (\w+)
\w+ match any word character [a-zA-Z0-9_]
Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
(?:-\w+)? Non-capturing group
Quantifier: ? Between zero and one time, as many times as possible, giving back as needed [greedy]
- matches the character - literally
\w+ match any word character [a-zA-Z0-9_]
Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
\. matches the character . literally
jpg matches the characters jpg literally (case insensitive)
i modifier: insensitive. Case insensitive match (ignores case of [a-zA-Z])