开发者

Regex modification - matching optionally dash (PHP)

I w开发者_JS百科ould like to ask you how to modify that regex:

"/\b([A-Z]{2,}[0-9][0-9A-Z-\/]*)/is"

as currently it can only match it if there would be not '-'. What I am trying to achieve is that in addition it can match something like that i.e.

TGW-1234    

so at least 2 letters followed by (optionally) dash and then (required) digits.


use a -? between the letters and numbers. ? means optional

"/\b([A-Z]{2,}-?[0-9][0-9A-Z-\/]*)/is"

It is placed after the character that is allowed to appear at that point in the expression, but who's existence isn't actually required to still be considered a successful match.


"/\b([A-Z]{2,}-?[0-9][0-9A-Z-\/]*)/is"


"/\b([A-Z]{2,}-?[0-9][0-9A-Z-\/]*)/is"
--------------^^ optional dash
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜