Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
'test' contains string, which checks for a particular 'pattern'. If pattern doesn't exist, find at least a character of pattern and it's position and re-frame the 'test' consisting of provided pattern.

1) In scenario 1, pattern exists. So, no replacement.
Python
test = "aebfz"
pattern = "ebf"
output = No replacement required


2) In scenario 2, no pattern exists but 'b' exists. After re-framing, out put should look like below (position of 'b' in test is 1. 'b' in pattern is '1')
Python
test = "abcdz" 
pattern = "ebf"        
output = "ebfdz"


3) In Scenario 3, no pattern exists. But both 'b' and 'e' exists at different positions. So, possible output looks like below ('b' is '0' and 'e' is '4' in test)
Python
test = "bacde"
pattern = "ebf"
output1 = "ebfcde" (added 'e' before 'b')
output2 = "bacdebf" (added 'bf' after 'e')


4) In scenario 4, no pattern exists. But char 'b' exists, so as output.
Python
test = "xabdz"   
pattern = "ebf"    
Output = "xebfz"


5) In scenario 5, no pattern exists. Output can be re-framed as below
Python
test = "vwxyz"
pattern = "ebf"
Output1 = "ebfyz"
Output2 = "vebfz"
Output3 = "vwebf"


I'm thinking to mask and replace mechanism. For example: scenario 2

Python
test = "abcdz" 
pattern = "ebf" 
new_test = "#b#dz" (should look like)
final_test = "ebfdz"


I don't know how it can be done. Just my approach. Can you please help me here ? If my approach is not right, please let me know possible best option.

What I have tried:

<pre lang="Python">test = "abcdz" 
pattern = "ebf" 
new_test = "#b#dz" (should look like)
final_test = "ebfdz"
Posted
Updated 20-Aug-20 5:18am

1 solution

See 3. An Informal Introduction to Python — Python 3.7.9 documentation[^] for how to address elements of a string. Further pages will explain string methods, list comprehensions etc.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900