I am using Maven and I have a lot of dependencies I want to remove. I am trying to automate the process for future use, using Powershell and replacing the dependencies using a regex (replacing with an empty string). I have dependencies littered throughout my pom file like so:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
The current iteration of the regex is as follows:
(<dependency>)(\s*?\S*?\w*?\W*?.*?\X*?\R*?\v*?)(spring-jdbc)(\s*?\S*?\w*?\W*?.*?\X*?\R*?\v*?)(<\/dependency>)
Using the preceding regex with "spring-jdbc" I can successfully find the dependency provided it is the first one encountered. If I switch "spring-jdbc" to "spring-core", the entire the text is selected. I tried inserting negative lookaheads/behinds to try and exclude dependency tags within the pattern like so:
(<dependency>)((?!<dependency>)\s*?\S*?\w*?\W*?.*?\X*?\R*?\v*?)(spring-core)(\s*?\S*?\w*?\W*?.*?\X*?\R*?\v*?(?<!<dependency>))(<\/dependency>)
But this only stops the tags appearing immediately after the start tag and immediately before the end tag. I want the entire gap between the start dependency tag and dependency name to not include an extra start dependency tag, and the same for the gap between the dependency name and end dependency tag but this time excluding an extra end dependency tag.
A link to regex101 example.
As it stands, I am getting the impression that Powershell/regexes were not intended for this kind of task. I would probably be better off creating a Java program or something like that to read the XML but for the sake of learning Powershell, I would like to know if it's possible. There are similar examples already but few (if any) seem to have the requirement to have a known constant in the center of the regex as well as excluding words between the endpoints of the tags (most XML/HTML examples I have seen just want all the characters in the tag bodies).
Thanks for any assistance.