Finding the desired links via Regex

Viewed 131

I am a beginner in Regex but I am trying to implement it. What I am trying to do is I want to extract the links that are present in specific files via regex and print them out. Below are the links all in one place that I want to extarct:

https://ci.adoptopenjdk.net/view/Test_openjdk/job/openjdk8_hs_openjdktest_s390x_linux/18/consoleFull

https://ci.adoptopenjdk.net/view/Test_openjdk/job/Test_openjdk11_hs_sanity.openjdk_x86-64_mac/26/consoleFull

To reproduce: https://ci.adoptopenjdk.net/job/Test_openjdk14_hs_sanity.openjdk_ppc64_aix/111/console

Jenkins Build URL: https://ci.adoptopenjdk.net/job/Test_openjdk8_hs_extended.system_ppc64le_linux/259/

internal build `Test_openjdk8_j9_sanity.openjdk_aarch64_linux/43/`

internal build `Test_openjdk11_j9_sanity.functional_aarch64_linux/46` 

I want the links in the form of (Expected):

https://ci.adoptopenjdk.net/view/Test_openjdk/job/openjdk8_hs_openjdktest_s390x_linux/18/
https://ci.adoptopenjdk.net/view/Test_openjdk/job/Test_openjdk11_hs_sanity.openjdk_x86-64_mac/26/
https://ci.adoptopenjdk.net/job/Test_openjdk14_hs_sanity.openjdk_ppc64_aix/111/
Test_openjdk8_j9_sanity.openjdk_aarch64_linux/43/

To solve it I tried to apply regex concept and came up with this pattern: (?:(?:http|ftp|https):\/\/ci.adoptopenjdk.net.+(?:consoleFull|console|\d?))|(?:\`.+?\`)

But the output comes like this (Actual):

https://ci.adoptopenjdk.net/job/Test_openjdk17_j9_sanity.openjdk_x86-64_linux/33/consoleFull

https://ci.adoptopenjdk.net/view/Test_openjdk/job/openjdk8_hs_openjdktest_s390x_linux/18/consoleFullhttps://ci.adoptopenjdk.net/job/Test_openjdk11_hs_sanity.openjdk_ppc64_aix/145/`Test_openjdk8_j9_sanity.openjdk_aarch64_linux/43/`
`cent7-aarch64-3``Test_openjdk8_j9_sanity.openjdk_aarch64_linux/43/`
`cent7-aarch64-3`

Now I wanted to check on the basis of https://....Test_openjdk{num} as ci.adoptopenjdk.net is outdated To encounter this situation I made a regex pattern to check all the links as mentioned above as inputs on the basis of Test_openjdk{num} to get the desired output. I used

(?:http|ftp|https):\/\/Test_openjdk/\d.+\/(?=console)|(?<=\`).+?\/(?=\`)

but only got the following output:

Test_openjdk8_j9_sanity.openjdk_aarch64_linux/43/
Test_openjdk8_j9_sanity.openjdk_aarch64_linux/43/

I have been trying various ways but they all are failing. Can anyone explain me where and why am I going wrong it would really be great. Thank You

1 Answers
(?:(?:http|ftp|https):\/\/ci\.adoptopenjdk\.net.+\/|(?:\`.+?\`))

.+\/ will make sure you stop the match at the last / in your link

also . means match any character if you wanna match "." exactly use \.

If you only want the ones that end in console or /' and your regex flavor supports positive lookahead and positive lookbehind you can use this

(?:http|ftp|https):\/\/ci\.adoptopenjdk\.net.+\/(?=console)|(?<=\`).+?\/(?=\`)

(?=console) means it has to be followed by string console but it will not capture it

(?<=\`) means that there has to be a backtick before the match but it will not capture it

(?=\`) means that there has to a backtick after the match but it will not capture it

regarding your python function problem you need to join with \n and print the result of the function

import re
def regexify(s):
    pattern = r"(?:http|ftp|https):\/\/ci\.adoptopenjdk\.net.+\/(?=console)|(?<=\`).+?\/(?=\`)"
    substring = re.findall(pattern, s)
    result='\n'.join(substring)
    if result:
        try:
            return result
        except:
            return ' '
x='''
https://ci.adoptopenjdk.net/view/Test_openjdk/job/openjdk8_hs_openjdktest_s390x_linux/18/consoleFull

https://ci.adoptopenjdk.net/view/Test_openjdk/job/Test_openjdk11_hs_sanity.openjdk_x86-64_mac/26/consoleFull

To reproduce: https://ci.adoptopenjdk.net/job/Test_openjdk14_hs_sanity.openjdk_ppc64_aix/111/console

Jenkins Build URL: https://ci.adoptopenjdk.net/job/Test_openjdk8_hs_extended.system_ppc64le_linux/259/

internal build `Test_openjdk8_j9_sanity.openjdk_aarch64_linux/43/`

internal build `Test_openjdk11_j9_sanity.functional_aarch64_linux/46` 
'''
print(regexify(x))

Output:

https://ci.adoptopenjdk.net/view/Test_openjdk/job/openjdk8_hs_openjdktest_s390x_linux/18/
https://ci.adoptopenjdk.net/view/Test_openjdk/job/Test_openjdk11_hs_sanity.openjdk_x86-64_mac/26/
https://ci.adoptopenjdk.net/job/Test_openjdk14_hs_sanity.openjdk_ppc64_aix/111/
Test_openjdk8_j9_sanity.openjdk_aarch64_linux/43/
Related