Trying to create an update script for Minecraft mods and need to be able to remove all the version numbers from the mods to search for the new version.
I have gotten it working to a point but am stuck on one last part. I cannot get it to leave out the 25 from pre25 from that line I need cfm pre (the 25 is the build number which I do not want)
[A-Za-z].*?(?=-[0-9])|(?<= ).*(?= )|([s].*?[k])|([f].*?[e])
1.17.1-additionalbars-2.1.0.jar
'[1.17.1] SecurityCraft v1.8.23-beta5.jar'
absentbydesign-1.17.1-1.6.0.jar
additionalbarsbop-2.1.0.jar
AdditionalEnchantedMiner-1.17-17.6-SNAPSHOT.jar
additionallanterns-1.0.0a-mc1.17.jar
cfm-7.0.0pre25-1.17.1.jar
Croptopia-1.17-FORGE-1.6.2.jar
nekoration-1.17.X-1.4.0.jar
ScalableCatsForce-2.13.6-build-4-with-library.jar
spark-forge.jar
treeharvester_1.17.1-3.2.jar
CodePudding user response:
I found it difficult to extract "pre" just by the pattern. At the same spot which "pre" is at there may also be "build" or "mc1" or whichever naming convention you happen to not need.
I made a different regular expression with less backwards and forwards jumping, which seems to find the mod names just fine and using a capture group to find "pre" explicitly.
See working example on regex101
/^([\[\]\'\.0-9 -]*)([a-zA-Z0-9]*)[-_]*([v\.0-9 -]*)(pre|FORGE)?([\.0-9 a-zA-Z-]*)\.jar.*/
Input
1.17.1-additionalbars-2.1.0.jar
'[1.17.1] SecurityCraft v1.8.23-beta5.jar'
absentbydesign-1.17.1-1.6.0.jar
additionalbarsbop-2.1.0.jar
AdditionalEnchantedMiner-1.17-17.6-SNAPSHOT.jar
additionallanterns-1.0.0a-mc1.17.jar
cfm-7.0.0pre25-1.17.1.jar
Croptopia-1.17-FORGE-1.6.2.jar
nekoration-1.17.X-1.4.0.jar
ScalableCatsForce-2.13.6-build-4-with-library.jar
spark-forge.jar
treeharvester_1.17.1-3.2.jar
Results
additionalbars
SecurityCraft
absentbydesign
additionalbarsbop
AdditionalEnchantedMiner
additionallanterns
cfm pre
Croptopia FORGE
nekoration
ScalableCatsForce
spark
treeharvester
I have used (pre|FORGE)
as an example which words to search for explicitly.
It works well enough for this case. You can use the regex101 link to experiment with it if you like.
CodePudding user response:
This part in your pattern is matching too much, as this part .*?(?=-[0-9])
is matching as least as possible chars until it can assert -[0-9]
to the right which will match pre25 in pre25-1
You could update the pattern to match an optional part matching digits separated by dots, and in the positive lookahead add asserting optional digits at the beginning [A-Za-z_] (?:\d (?:\.\d ) )?(?=[0-9]*-[0-9])
to reach the hyphen followed by a digit.
You could make the .*?
non greedy matching all between spaces as .*
can over match in case of more spaces.
The updated pattern (without the capture groups if you want a match only) could look like
[A-Za-z_] (?:\d (?:\.\d ) )?(?=[0-9]*-[0-9])|(?<= ).*?(?= )|s.*?k|f.*?e