Replacement Problem

Discussion in 'Programming' started by juhav7, Apr 29, 2022.

  1. #1
    Is there any way or program to solve this kind of replacement. I have the text file which contains:

    <a>word1</a><b>---</b>

    <a>word2</a><b>---</b>

    <a>word3</a><b>---</b>

    etc... and lot of other text

    I want to replace first --- with word1, second --- with word2, etc

    Those words can be any kind of words like flower, people, beer... but the brackets (<a></a><b></b>) are the same.

    Is it possible to make all replacements AT ONCE? And how?

    The program like PowerGREP maybe could do this but I don't know how.

    Thanks if you can help.
     
    juhav7, Apr 29, 2022 IP
  2. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #2
    Are the bold tags always going to be the next element? Is there a container you can hook those anchors off of without pissing clasess onto all the children?

    <div class="moveA2B">
      <a>word1</a><b>---</b>
      <a>word1</a><b>---</b>
      <a>word1</a><b>---</b>
      <a>word1</a><b>---</b>
    <!-- .moveA2B --></div>
    Code (markup):
    then for the scripting:
    
    for (let anchor of document.querySelectorAll("moveA2B a") {
      anchor.nextElementSibling.textContent = anchor.textContent;
    }
    
    Code (markup):
    Though I would question why you'd be doing this. Moving anchor text to a sibling element seems very strange.
     
    deathshadow, Apr 30, 2022 IP
  3. mmerlinn

    mmerlinn Prominent Member

    Messages:
    3,197
    Likes Received:
    819
    Best Answers:
    7
    Trophy Points:
    320
    #3
    Since you have a text file, just about any program that massages text files will do your job with a FOR/NEXT or DO WHILE/ENDDO loop.

    If it were me, I would write FoxPro function with a few lines of code to do the job. Same can be done with any other language supporting text files.
     
    mmerlinn, Apr 30, 2022 IP