Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6380793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:18:48+00:00 2026-05-25T02:18:48+00:00

My programming knowledge is very limited, I would really appreciate any help on this

  • 0

My programming knowledge is very limited, I would really appreciate any help on this possibly obvious problem!

Lets say I have a text file, that somewhere contains the text: “I own two (Some text in between…) bicycles.”

How could I for example change two to three? Meaning I need a function to find the string “bicycles” and then look to the left until it somewhere finds the string “two” and changes that.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T02:18:49+00:00Added an answer on May 25, 2026 at 2:18 am

    With regular expressions:

    import re
    
    line = '-------------------------------------------------------------\n'
    
    ss = ('I gave two similar things to my two twin sons: '
          'two spankings, two nice BICYCLES of 300 dollars each, '
          'yes 600 dollars for two horridly nice BICYCLES, '
          'two times 300 dollars for two sons receiving two BICYCLES !, '
          'two dollars too, but never two dogs')
    print ss,'\n\n'
    
    
    print line + '1) Replacing the more at right before the first "BICYCLES":\n'
    reg = re.compile('two(?=(?:.(?!two))*?BICYCLES)(.+)')
    print reg.sub('@@@@\\1',ss)
    
    
    print line + '2) Replacing the more at right before the last "BICYCLES":\n'
    reg = re.compile('two(?=(?:.(?!two))*?BICYCLES(?!.*?BICYCLES))')
    print reg.sub('@@@@',ss)
    
    
    print line + '3) Replacing all before the first "BICYCLES":\n'
    reg = re.compile('(two)|BICYCLES.+')
    print reg.sub(lambda mat: '@@@@' if mat.group(1) else mat.group(),ss)
    
    
    print line + '4) Replacing all before the last "BICYCLES":\n'
    reg = re.compile('(two)|BICYCLES(?!.*?BICYCLES).+')
    print reg.sub(lambda mat: '@@@@' if mat.group(1) else mat.group(),ss)
    

    result

    I gave two similar things to my two twin sons: two spankings, two nice BICYCLES of 300 dollars each, yes 600 dollars for two horridly nice BICYCLES, two times 300 dollars for two sons receiving two BICYCLES !, two dollars too, but never two dogs 
    
    
    -------------------------------------------------------------
    1) Replacing the more at right before the first "BICYCLES":
    
    I gave two similar things to my two twin sons: two spankings, @@@@ nice BICYCLES of 300 dollars each, yes 600 dollars for two horridly nice BICYCLES, two times 300 dollars for two sons receiving two BICYCLES !, two dollars too, but never two dogs
    -------------------------------------------------------------
    2) Replacing the more at right before the last "BICYCLES":
    
    I gave two similar things to my two twin sons: two spankings, two nice BICYCLES of 300 dollars each, yes 600 dollars for two horridly nice BICYCLES, two times 300 dollars for two sons receiving @@@@ BICYCLES !, two dollars too, but never two dogs
    -------------------------------------------------------------
    3) Replacing all before the first "BICYCLES":
    
    I gave @@@@ similar things to my @@@@ twin sons: @@@@ spankings, @@@@ nice BICYCLES of 300 dollars each, yes 600 dollars for two horridly nice BICYCLES, two times 300 dollars for two sons receiving two BICYCLES !, two dollars too, but never two dogs
    -------------------------------------------------------------
    4) Replacing all before the last "BICYCLES":
    
    I gave @@@@ similar things to my @@@@ twin sons: @@@@ spankings, @@@@ nice BICYCLES of 300 dollars each, yes 600 dollars for @@@@ horridly nice BICYCLES, @@@@ times 300 dollars for @@@@ sons receiving @@@@ BICYCLES !, two dollars too, but never two dogs
    

    .

    It is also possible without regular expressions:

    line = '-------------------------------------------------------------\n'
    
    ss = ('I gave two similar things to my two twin sons: '
          'two spankings, two nice BICYCLES of 300 dollars each, '
          'yes 600 dollars for two horridly nice BICYCLES, '
          'two times 300 dollars for two sons receiving two BICYCLES !, '
          'two dollars too, but never two dogs')
    print ss,'\n\n'
    
    
    print line + '1) Replacing the more at right before the first "BICYCLES":\n'
    fb = ss.find('BICYCLES')
    print '@@@@'.join(ss[0:fb].rsplit('two',1)) + ss[fb:] if fb+1 else ss
    
    
    print line + '2) Replacing the more at right before the last "BICYCLES":\n'
    fb = ss.rfind('BICYCLES')
    print '@@@@'.join(ss[0:fb].rsplit('two',1)) + ss[fb:] if fb+1 else ss
    
    
    print line + '3) Replacing all before the first "BICYCLES":\n'
    fb = ss.find('BICYCLES')
    print ss[0:fb].replace('two','@@@@') + ss[fb:] if fb+1 else ss
    
    
    print line + '4) Replacing all before the last "BICYCLES":\n'
    fb = ss.rfind('BICYCLES')
    print ss[0:fb].replace('two','@@@@') + ss[fb:] if fb+1 else ss
    

    results are the same

    .

    But using regular expressions give more possibilities:

    import re
    
    ss = ('Mr Dotwo bought two gifts for his two sons, two hours ago: two BICYCLES '
          'because his two sons wanted only two BICYCLES')
    print ss,'\n\n'
    
    
    print 'Replacing all "two" before the first "BICYCLES":\n'
    reg = re.compile('(\\btwo\\b)|BICYCLES.+')
    print reg.sub(lambda mat: '@@@@' if mat.group(1) else mat.group(),ss)
    

    result

    Mr Dotwo bought two gifts for his two sons, two hours ago: two BICYCLES because his two sons wanted only two BICYCLES 
    
    
    Replacing all strings "two" before the first "BICYCLES":
    
    Mr Dotwo bought @@@@ gifts for his @@@@ sons, @@@@ hours ago: @@@@ BICYCLES because his two sons wanted only two BICYCLES
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I do not have very much knowledge of programming and really need help. I
I'm unfamiliar with asyncore , and have very limited knowledge of asynchronous programming except
I have very little programming knowledge; only a fair bit in Visual Basic. How
I have a very basic knowledge of the web programming, and I couldn't figure
Please forgive my programming knowledge. I know this is a simple thing, but I
I've been messing around with my very basic C++ knowledge (been programming for two
This is not really a technical programming question, but has more to do with
I'm very new to iPhone programming, so my problem might be caused by total
I have some knowledge about Human computer interaction and some basic knowledge programming scripts
Hey guys, I'm very excited about how experienced I am in programming. The first,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.