fork download
  1. import re
  2.  
  3. versions = [
  4. #production .net core standards
  5. "3.9.1.0+3f8b21d3a442bb4371e73e5c78f00191f437ab92.0!",
  6.  
  7. #old pipeline model
  8. "3.9.0.113.1",
  9.  
  10. #pipeline model
  11. "3.10.0.27.9ec628f87baab65d7b0a809ce3ac6ca4749b7b77"
  12. ]
  13.  
  14. stringPattern= r"^(\d+\.\d+\.\d+\.\d+)"
  15. pattern = re.compile(stringPattern)
  16. print("===================example 1===============================")
  17. for version in versions:
  18. result = pattern.match(version)
  19. if result:
  20. print(result.group(1))
  21.  
  22.  
  23. print("===================example 2===============================")
  24. for version in versions:
  25. result = re.search(pattern, version)
  26. print(result.group())
  27.  
  28. print("=================== Option 3 ===============================")
  29. extracted_versions = [
  30. match.group(1)
  31. for version in versions
  32. for match in [re.search(r"^(\d+\.\d+\.\d+\.\d+)", version)]
  33. if match
  34. ]
  35.  
  36. for version in extracted_versions:
  37. print(version)
  38.  
  39. print("=================== Option 4 ===============================")
  40. for version in versions:
  41. matches = re.findall(r"^(\d+\.\d+\.\d+\.\d+)", version)
  42. if matches:
  43. print(matches[0])
  44.  
  45.  
  46. print("=================== Option 5 ===============================")
  47. for version in versions:
  48. for match in re.finditer(r"^(\d+\.\d+\.\d+\.\d+)", version):
  49. print(match.group(1))
  50.  
  51.  
  52.  
  53.  
  54. print("=================== Option 6 ===============================")
  55. def extract_version(version_str, pattern):
  56. match = pattern.match(version_str)
  57. return match.group(1) if match else None
  58. # Use map to apply the function to each version, then filter out None results.
  59. extracted = filter(None, map(lambda v: extract_version(v, pattern), versions))
  60. for version in extracted:
  61. print(version)
  62.  
Success #stdin #stdout 0.14s 15548KB
stdin
Standard input is empty
stdout
===================example 1===============================
3.9.1.0
3.9.0.113
3.10.0.27
===================example 2===============================
3.9.1.0
3.9.0.113
3.10.0.27
=================== Option 3 ===============================
3.9.1.0
3.9.0.113
3.10.0.27
=================== Option 4 ===============================
3.9.1.0
3.9.0.113
3.10.0.27
=================== Option 5 ===============================
3.9.1.0
3.9.0.113
3.10.0.27
=================== Option 6 ===============================
3.9.1.0
3.9.0.113
3.10.0.27