Здесь мы можем просто захотеть обернуть первую часть группой захвата:
# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"(Technology libraries: )(.*)$"
test_str = "Technology libraries: Techlibhellohellohello"
subst = "\\1\\n\\2"
# You can manually specify the number of replacements by changing the 4th argument
result = re.sub(regex, subst, test_str, 0, re.MULTILINE)
if result:
print (result)
# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.
В этой демонстрации JavaScript показано, как работают группы захвата:
const regex = /(Technology libraries: )(.*)$/gm;
const str = `Technology libraries: Techlibhellohellohello`;
const subst = `\n$1\n$2`;
// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);
console.log('Substitution result: ', result);
RegEx
Если это не ваше желаемое выражение, вы можете изменить / изменить выражения в regex101.com .
(Technology libraries: )(.*)
RegEx Circuit
Вы также можете визуализировать свои выражения в jex.im :
Если вы хотите удалить :
и пробелы, вы можете просто добавить среднюю группу захвата, которая делает это:
(Technology libraries)(:\s+)(.*)
Код Python
# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"(Technology libraries)(:\s+)(.*)"
test_str = ("Technology libraries: Techlibhellohellohello\n"
"Technology libraries: Techlibhellohellohello")
subst = "\\1\\n\\3"
# You can manually specify the number of replacements by changing the 4th argument
result = re.sub(regex, subst, test_str, 0, re.MULTILINE)
if result:
print (result)
# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.
Демонстрация JavaScript
const regex = /(Technology libraries)(:\s+)(.*)/gm;
const str = `Technology libraries: Techlibhellohellohello
Technology libraries: Techlibhellohellohello`;
const subst = `\n$1\n$3`;
// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);
console.log('Substitution result: ', result);
Если вы хотите захватить пробелы перед «Библиотеками технологий», вы можете просто добавить их в группу захвата:
^(\s+)(Technology libraries)(:\s+)(.*)$
Python Test
# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"^(\s+)(Technology libraries)(:\s+)(.*)$"
test_str = (" Technology libraries: Techlibhellohellohello\n"
" Technology libraries: Techlibhellohellohello")
subst = "\\2\\n\\4"
# You can manually specify the number of replacements by changing the 4th argument
result = re.sub(regex, subst, test_str, 0, re.MULTILINE)
if result:
print (result)
# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.
JavaScript Demo
const regex = /^(\s+)(Technology libraries)(:\s+)(.*)$/gm;
const str = ` Technology libraries: Techlibhellohellohello
Technology libraries: Techlibhellohellohello`;
const subst = `$2\n$4`;
// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);
console.log('Substitution result: ', result);