Based on a REGEX pattern set a value using Groovy in Jenkins pipeline - regex

Based on a REGEX validation I am trying to set a value which is not happening as expected in Groovy Jenkins pipeline
If version = 1.1.1 or 1.5.9 or 9.9.9 like that then it has to be a managed store else like 1.22.9 or 1.99.9 ... tmp store. Below is what I tried with
String artefact_bucket_name
def artefact_version = "1.99.0"
if (artefact_version ==~ /[0-9]{1}\.[0-9]{1}\.[0-9]{1}/) {
artefact_bucket_name = "managed-artefact-store"
}
if (artefact_version ==~ /[0-9]{1}\.[0-9]{1,2}\.[0-9]{1}/) {
artefact_bucket_name = "tmp-artefact-store"
}
echo "Application version to deploy is ${artefact_version} from Artefact store ${artefact_bucket_name}"

It looks like you have a mistake in the second regex, that is overriding first one. e.g. when you have artefact_version = 1.1.1 - It matches first regex and second regex as well, so it always will be tmp-artefact-store.
I would change the second regex to match string like:
/[0-9]{1}\.[0-9]{2}\.[0-9]{1}/ - Notice I changed {1,2} to only {2}. This will exclusively match strings like "\d.\d\d.\d", so version like 1.1.1 will match only first regex and version like 1.99.9 - only second

Related

Kotlin regular expression has no match

I am practicing regular expressions in Kotlin and trying to start with a simple string. However, I am not receiving any matches. I feel like I am doing it right and can't figure out the problem.
Test String:
VERSION_ID="12.2"
And what would I do this for multiple lines:
NAME="SLED"
VERSION="12-SP2"
VERSION_ID="12.2"
PRETTY_NAME="SUSE Linux Enterprise Desktop 12 SP2"
ID="sled"
ID_LIKE="suse"
ANSI_COLOR="0;32"
CPE_NAME="cpe:/o:suse:sled:12:sp2"
Regex:
private val myRegex = Regex("VERSION_ID=(\\d+)\\.(\\d+)")
I am simply trying to match 12.2 from the test string.
https://regex101.com/r/9IG8cO/1
The version ID value inside your string appears to be surrounded with double quotes. Also, I suggest making the decimal portion optional, in case some versions might not have a minor version component:
private val myRegex = Regex("VERSION_ID=\"(\\d+(?:\\.\\d+)?)\"")

Using Regex to capture data in jenkins pipeline

I want to pull apart a docker image uri inside of my Jenkins pipeline. Example uri = https://test-registry.home.imagehub.com/engineering/images/rhel:latest. I would like to break this down into 4 parts.
https://(test-registry).home.imagehub.com/(engineering/images)/(rhel):(latest)
$1 = test-registry
$2 = engineering/images
$3 = rhel
$4 = latest
caveats $2 sometimes only has one directory level
I believe the proper regex for this is:
/\w+:\/\/(.*)\.[^\/]+/(.*)/(\w+):(.*)/
This should grab everything after the //s up to the first . (period) for $1
The [^\/] should move up to the / divider through the subdomains
I want the second (.*) to be greedy up to but not including the last / divider.
The 3rd part should grab everything after the last / divider and before the :
the 4th part should grab everything after the : (colon)
I did include the regex
import java.util.regex.Pattern
I have tried a lot of different solutions to make this work in my jenkins declarative pipeline such as inline:
sReg = https://test-registry.home.imagehub.com/engineering/images/rhel:latest
pipeline
agent
stages
stage ('regex')
steps
scripts
def sReg = https://test-registry.home.imagehub.com/engineering/images/rhel:latest
def registry = sReg =~ /\w+:\/\/(.*)\./
here I expected:
registry = test-registry
So I echo the result
echo registry.group(1)
but this gives me:
java.lang.IllegalStateException: No match found
Which makes no sense. I have tried just getting the https with /(\w+):/ but same result.
I tried with a class also but same result, no matches.
I would like to get all four in one shot but I will pull out each part in a separate regex if need be. At this point I would like to figure out how I can at least get a match.
I added:
import java.util.regex.Matcher
Then down in the script section I added:
def registry = Sreg =~ ///([^.]+)[^/]+/((?:[^/]+/)*[^/]+)/(.+):(.+)/
println(registry)
When I run the pipeline I get:
[Pipeline] echo
java.util.regex.Matcher[pattern=//([^.]+)[^/]+/((?:[^/]+/)*[^/]+)/(.+):(.+) region=0,78 lastmatch=]
So instead of extracting the information it is equaling the pattern?
I did an echo on Sreg and it shows the uri so it should be getting the uri in my def.
You can access groups with :
def sReg = "https://test-registry.home.imagehub.com/engineering/images/rhel:latest"
def registry = sReg =~ /\w+:\/\/(.*)\.[^\/]+/(.*)/(\w+):(.*)/
println registry[0] //For all groups
println registry[0][1] //For group 1
Sources and examples : https://mrhaki.blogspot.com/2009/09/groovy-goodness-matchers-for-regular.html
So it turns out my whole issue was my agent statement.
Instead of: agent { node 'slave' }
I had: agent {node 'slave'}
Once I fixed this the regex started working.
FYI
I found out also the println(registry) statement is still just the pattern matcher but now println(registry[0][1]) gives me the captured text.

Flutter cannot parse regex

Flutter cannot parse this working regex, and doesn't return any error or info.
(?<=id=)[^&]+
However, when I add it into my Flutter app:
print("before");
new RegExp(r'(?<=id=)[^&]+');
print("after");
It doesn't do anything, doesn't return any error. The print("after"); never gets executed. It doesn't completly freeze the app, because it's in async.
Dart compiled for the Web supports lokbehinds, but the current version of native Dart (including Flutter) does not support lookbehinds (source).
In your case, you want to match a string after a specific string. All you need is to declare a capturing group in your pattern and then access that submatch:
RegExp regExp = new RegExp(r"id=([^&]+)");
String s = "http://example.com?id=some.thing.com&other=parameter; http://example.com?id=some.thing.com";
Iterable<Match> matches = regExp.allMatches(s);
for (Match match in matches) {
print(match.group(1));
}
Output:
some.thing.com
some.thing.com
Here, id=([^&]+) matches id= and then the ([^&]+) capturing group #1 matches and captures into Group 1 any one or more chars other than &. Note you may make it safer if you add [?&] before id to only match id and not thisid query param: [?&]id=([^&]+).
I assume this is https://github.com/dart-lang/sdk/issues/34935
Bring Dart's RegExp support in line with JavaScript: lookbehinds, property escapes, and named groups.

Regex ignore first 12 characters from string

I'm trying to create a custom filter in Google Analytic to remove the query parts of the url which I don't want to see. The url has the following structure
[domain]/?p=899:2000:15018702722302::NO:::
I would like to create a regex which skips the first 12 characters (that is until:/?p=899:2000), and what ever is going to be after that replace it with nothing.
So I made this one: https://regex101.com/r/Xgbfqz/1 (which could be simplified to .{0,12}) , but I actually would like to skip those and only let the regex match whatever is going to be after that, so that I'll be able to tell in Google Analytics to replace it with "".
The part in the url that is always the same is
?p=[3numbers]:[0-4numbers]
Thank you
Your regular expression:
\/\?p=\d{3}\:\d{0,4}(.*)
Tested in Golang RegEx 2 and RegEx101
It search for /p=###:[optional:####] and capture the rest of the right side string.
(extra) JavaScript:
paragraf='[domain]/?p=899:2000:15018702722302::NO:::'
var regex= /\/\?p=\d{3}\:\d{0,4}(.*)/;
var match = regex.exec(paragraf);
alert('The rest of the right side of the string: ' + match[1]);
Easily use "[domain]/?p=899:2000:15018702722302::NO:::".substr(12)
You can try this:
/\?p\=\d{3}:\d{0,4}
Which matches just this: ?p=[3numbers]:[0-4numbers]
Not sure about replacing though.
https://regex101.com/r/Xgbfqz/1

how do I match this url

Hey, how do I match the url that starts with digits which are followed by "?fmt=json", like 1234?fmt=json return true but my another wep handler which handles the urls that are all digits like 1234 return false? I have tried \d+(?!\?fmt=json) which is supposed match the url where the digits are not followed by "?fmt=json", but it doesnt work. any helps? thank you
This regular expression only matches when the fmt=json suffix is there and will "return false" if only numbers:
\d+\?fmt=json
Like
http://something/1234?fmt=json == true, (match=1234?fmt=json)
http://something/1234 == false
See here in a regex tester
You can't match the query string in App Engine's webapp, or most other Python webapp frameworks. It's a rather odd thing to want to do, too - your handler should fetch the value of the argument, and modify its output based on that.