Это выражение имеет три группы захвата, где вторая имеет желаемый результат:
(https:\/\/www.myurl.com\/)([A-Za-z0-9-]+)(\/\smarker needle)
Этот инструмент помогает нам изменить / изменить выражение, если вы хотите.
Описательный график RegEx
jex.im визуализирует регулярные выражения:
Тест Python
# -*- coding: UTF-8 -*-
import re
string = "[text] something - https://www.myurl.com/test1/ lorem ipsum https://www.myurl.com/test2/ - https://www.myurl.com/test3/ marker needle - some more text at the end"
expression = r'(https:\/\/www.myurl.com\/)([A-Za-z0-9-]+)(\/\smarker needle)'
match = re.search(expression, string)
if match:
print("YAAAY! \"" + match.group(2) + "\" is a match ??? ")
else:
print('? Sorry! No matches!')
Выход
YAAAY! "test3" is a match ???
Тест производительности
Этот фрагмент возвращает время выполнения1 миллион for
петли.
const repeat = 10;
const start = Date.now();
for (var i = repeat; i >= 0; i--) {
const regex = /(.*)(https:\/\/www.myurl.com\/)([A-Za-z0-9-]+)(\/\smarker needle)(.*)/gm;
const str = "[text] something - https://www.myurl.com/test1/ lorem ipsum https://www.myurl.com/test2/ - https://www.myurl.com/test3/ marker needle - some more text at the end";
const subst = `$3`;
var match = str.replace(regex, subst);
}
const end = Date.now() - start;
console.log("YAAAY! \"" + match + "\" is a match ??? ");
console.log(end / 1000 + " is the runtime of " + repeat + " times benchmark test. ? ");