我需要通过regex-php在字符串中获取"v3.4.2"。字符串:"ABCDEFv3.4.2 GHI-KLMN";
使用可变长度和数字的安全RegEx:
'bv('d+'.)+'d+'b
Regex101上的实时演示
工作原理:
'b # Word Boundary
v # v
('d+'.) # Digit(s) followed by . - i.e. 3. or 4.
+ # Match many digit(s) followed by dot - i.e. 3.4.2. or 5.6.
'd+ # Final digit of version (not included above because it has no trailing .)
'b # Word Boundary
如果格式完全如图所示,请使用此较短的RegEx:
'bv'd'.'d'.'d'b
Regex101上的实时演示
'b
标记了一个单词边界,因此它不会在donotv3.4.2capturethis
内捕获
工作原理:
'b # Word Boundary
v # v
'd'.'d'.'d # 3.4.2
'b # Word Boundary