Home > Blockchain >  How to use regular expression to extract groupId, artifactid and version from maven coordinate?
How to use regular expression to extract groupId, artifactid and version from maven coordinate?

Time:02-05

I tried (. ?):(. )((?::). )? to test below:

androidx.hilt:hilt-compiler

  • group1: androidx.hilt
  • group2: hilt-compiler
  • group3:

androidx.hilt:hilt-compiler:1.2.3

  • group1: androidx.hilt
  • group2: hilt-compiler:1.2.3
  • group3:

I expect second one to be

  • group1: androidx.hilt
  • group2: hilt-compiler
  • group3: 1.2.3

I have tried different regular expression but none of them works.

CodePudding user response:

You might write the pattern as:

^([^:\n] ):([^:\n] )(?::([^\n:] ))?$

Explanation

  • ^ Start of string
  • ([^:\n] ) Group 1, match 1 chars other than : or a newline
  • :([^:\n] ) Match : followed by group 2 matching 1 chars other than : or a newline
  • (?: Non capture group
    • :([^\n:] ) Match : followed by group 3 matching 1 chars other than : or a newline
  • )? Close non capture group and make it optional
  • $ End of string

Regex demo

  • Related