如何通过正则表达式获取字符串中的“v.3.1.2”[重复]

This question already has an answer here:

I need get "v3.4.2" in string by regex php. String: "ABCDEF v3.4.2 GHI KLMN";

</div>

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