[[+content_image]]
N
N
Nikolay Baranenko2021-10-08 13:35:59
Java
Nikolay Baranenko, 2021-10-08 13:35:59

What should the correct regular expression look like to search for a hostname?

Groovy

def URL = "sdfgsdfg https://inclouds.bizmail.ru hgfghfhgfhgf gfhgf"

print(URL.findAll(/\/\/(\w+.\w+.\w+)/))


Returns:
[//inclouds.bizmail.ru][[://inclouds.bizmail, inclouds.bizmail]]


But I need inclouds.bizmail.ru.

How to do it right?

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
D
dollar, 2021-10-08
@drno-reg

At least the dot is not escaped, which means it is "any character". But this is so, by the way.
And the answer is:

def URL = "sdfgsdfg https://inclouds.bizmail.ru hgfghfhgfhgf gfhgf"

print(URL.findAll(/(?<=:\/\/)[\w\d.]+/))

Although it is far from perfect. More precise conditions of the problem will help to improve it.

V
vreitech, 2021-10-08
@fzfx

print(URL.findAll(~(?=https://)[^\s]+~))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question