How to get “v.3.1.2” in string by regex php [duplicate]

A safe RegEx to work with variable lengths and digits:

\bv(\d+\.)+\d+\b

Live Demo on Regex101

How it works:

\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

If the format is exactly as shown, use this shorter RegEx:

\bv\d\.\d\.\d\b

Live Demo on Regex101

\b marks a word boundary, so it will not capture inside donotv3.4.2capturethis

How it works:

\b             # Word Boundary
v              # v
\d\.\d\.\d     # 3.4.2
\b             # Word Boundary

Leave a Comment