Skip to main content
added 2 characters in body
Source Link
Dennis
  • 210.6k
  • 41
  • 370
  • 825

Python 3.5, 87 bytes

import re
lambda s,p,r:re.sub('(.*?)'.join(p),'\g<%d>'.join(r)%(*range(1,len(r)),),s,1)

repl.it to verify all test cases.

How it works

  • '(.*?)'.join(p) builds a search pattern that matches the subsequence to be replaced and anything between its elements.

    Since the modifiersquantifiers are lazy, each (.*?) will match as few characters as possible.

    For the pattern ghost, the constructed regex is g(.*?)h(.*?)o(.*?)s(.*?)t.

  • '\g<%d>'.join(r)%(*range(1,len(r)),) builds the replacement string, using string formatting.

    Each \g<n> refers to the nth captured group, just like \n would.

    For the replacement 12345, the constructed string is 1\g<1>2\g<2>3\g<3>4\g<4>5.

  • re.sub(...,...,s,1) performs at most one replacement in the string s.

Python 3.5, 87 bytes

import re
lambda s,p,r:re.sub('(.*?)'.join(p),'\g<%d>'.join(r)%(*range(1,len(r)),),s,1)

repl.it to verify all test cases.

How it works

  • '(.*?)'.join(p) builds a search pattern that matches the subsequence to be replaced and anything between its elements.

    Since the modifiers are lazy, each (.*?) will match as few characters as possible.

    For the pattern ghost, the constructed regex is g(.*?)h(.*?)o(.*?)s(.*?)t.

  • '\g<%d>'.join(r)%(*range(1,len(r)),) builds the replacement string, using string formatting.

    Each \g<n> refers to the nth captured group, just like \n would.

    For the replacement 12345, the constructed string is 1\g<1>2\g<2>3\g<3>4\g<4>5.

  • re.sub(...,...,s,1) performs at most one replacement in the string s.

Python 3.5, 87 bytes

import re
lambda s,p,r:re.sub('(.*?)'.join(p),'\g<%d>'.join(r)%(*range(1,len(r)),),s,1)

repl.it to verify all test cases.

How it works

  • '(.*?)'.join(p) builds a search pattern that matches the subsequence to be replaced and anything between its elements.

    Since the quantifiers are lazy, each (.*?) will match as few characters as possible.

    For the pattern ghost, the constructed regex is g(.*?)h(.*?)o(.*?)s(.*?)t.

  • '\g<%d>'.join(r)%(*range(1,len(r)),) builds the replacement string, using string formatting.

    Each \g<n> refers to the nth captured group, just like \n would.

    For the replacement 12345, the constructed string is 1\g<1>2\g<2>3\g<3>4\g<4>5.

  • re.sub(...,...,s,1) performs at most one replacement in the string s.

added 642 characters in body
Source Link
Dennis
  • 210.6k
  • 41
  • 370
  • 825

Python 3.5, 87 bytes

import re
lambda s,p,r:re.sub('(.*?)'.join(p),'\g<%d>'.join(r)%(*range(1,len(r)),),s,1)

repl.it to verify all test cases.

How it works

  • '(.*?)'.join(p) builds a search pattern that matches the subsequence to be replaced and anything between its elements.

    Since the modifiers are lazy, each (.*?) will match as few characters as possible.

    For the pattern ghost, the constructed regex is g(.*?)h(.*?)o(.*?)s(.*?)t.

  • '\g<%d>'.join(r)%(*range(1,len(r)),) builds the replacement string, using string formatting.

    Each \g<n> refers to the nth captured group, just like \n would.

    For the replacement 12345, the constructed string is 1\g<1>2\g<2>3\g<3>4\g<4>5.

  • re.sub(...,...,s,1) performs at most one replacement in the string s.

Python 3.5, 87 bytes

import re
lambda s,p,r:re.sub('(.*?)'.join(p),'\g<%d>'.join(r)%(*range(1,len(r)),),s,1)

repl.it to verify all test cases.

Python 3.5, 87 bytes

import re
lambda s,p,r:re.sub('(.*?)'.join(p),'\g<%d>'.join(r)%(*range(1,len(r)),),s,1)

repl.it to verify all test cases.

How it works

  • '(.*?)'.join(p) builds a search pattern that matches the subsequence to be replaced and anything between its elements.

    Since the modifiers are lazy, each (.*?) will match as few characters as possible.

    For the pattern ghost, the constructed regex is g(.*?)h(.*?)o(.*?)s(.*?)t.

  • '\g<%d>'.join(r)%(*range(1,len(r)),) builds the replacement string, using string formatting.

    Each \g<n> refers to the nth captured group, just like \n would.

    For the replacement 12345, the constructed string is 1\g<1>2\g<2>3\g<3>4\g<4>5.

  • re.sub(...,...,s,1) performs at most one replacement in the string s.

added 73 characters in body
Source Link
Dennis
  • 210.6k
  • 41
  • 370
  • 825

Python 3.5, 87 bytes

import re
lambda s,p,r:re.sub('(.*?)'.join(p),'\g<%d>'.join(r)%(*range(1,len(r)),),s,1)

repl.it to verify all test cases.

Python 3.5, 87 bytes

import re
lambda s,p,r:re.sub('(.*?)'.join(p),'\g<%d>'.join(r)%(*range(1,len(r)),),s,1)

Python 3.5, 87 bytes

import re
lambda s,p,r:re.sub('(.*?)'.join(p),'\g<%d>'.join(r)%(*range(1,len(r)),),s,1)

repl.it to verify all test cases.

deleted 8 characters in body
Source Link
Dennis
  • 210.6k
  • 41
  • 370
  • 825
Loading
added 11 characters in body
Source Link
Dennis
  • 210.6k
  • 41
  • 370
  • 825
Loading
Source Link
Dennis
  • 210.6k
  • 41
  • 370
  • 825
Loading