Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

Search and replicate strings

Last post 02-08-2010, 11:37 AM by mash. 6 replies.
Sort Posts: Previous Next
  •  02-05-2010, 1:41 AM 59317

    Search and replicate strings

    Language: JavaScript ECMA-262 regular expressions

    This might be a very simple problem but I'm having a difficulty with regular expression, all I want is to search a string store it to $1 then replicate the same string on each target lines
    For example, find a string inside quotes of line L1 (Replimoi), and replicate the same string at the beginning of each target lines L2, L3, L4, but not L5.


    Sample Entry:

    L1  Authors name is ="Replimoi"
    L2  wrote the book of all books
    L3  painted the childrens illustration chart
    L4  coded the biomechanical ultimate progy
    L5  run out of books


    Output after Regex "find" and "replace" function:

    L1  Authors name is ="Replimoi"
    L2  Replimoi wrote the book of all books
    L3  Replimoi painted the childrens illustration chart
    L4  Replimoi coded the biomechanical ultimate progy
    L5  run out of books


    Appreciate any feedback, or any other language implementation is fine too (i.e. PCRE, VBScript)
    thanks.

    Liz
    Filed under:
  •  02-05-2010, 10:19 AM 59324 in reply to 59317

    Re: Search and replicate strings

    in C# : tested OK in Expresso 3.0:

    try this match pattern

    (?sm)(.*?\x22([^\x22]+)\x22.*?\r\n)(.*?\r\n)(.*?\r\n)(.*?\r\n)(.*?\r\n)

    followed by this replace pattern

    $1$2 $3$2 $4$2 $5$6

    outputs after replace:

    Authors name is ="Replimoi"
    Replimoi wrote the book of all books
    Replimoi painted the childrens illustration chart
    Replimoi coded the biomechanical ultimate progy
    run out of books

     

  •  02-05-2010, 11:15 AM 59325 in reply to 59317

    Re: Search and replicate strings

    liz_mm50:
    Language: JavaScript ECMA-262 regular expressions


    Appreciate any feedback, or any other language implementation is fine too (i.e. PCRE, VBScript)
    thanks.

    FYI:  Regexes are not portable in many cases.  Some regexes written for one language won't work at in another. In many cases the syntax is different. That's why we ask you to tell us which language you are working with in the Posting Guidelines. Perfect example Sergei's pattern uses a feature unsupported by JavaScript, but since you asked you got a pattern that wouldn't work as written in your target language. While the difference is minor and easy to fix if you know what you are looking for it is still easy to overlook.

    I assumed L# was actually part of the input. If not simply remove them from the pattern.

    Raw Match Pattern:
    (L1.*?\x22([^\x22]+)\x22.*\s^L2 )(.*\s^L3 )(.*\s^L4 )(.*?\s)

    Raw Replace Pattern:
    $1$2 $3$2 $4$2 $5

    Javascript Code Example:

    <script type="text/javascript">
      var re = /(L1.*?\x22([^\x22]+)\x22.*\s^L2 )(.*\s^L3 )(.*\s^L4 )(.*?\s)/m;
      var sourcestring = "source string to match with pattern";
      var replacementpattern = "$1$2 $3$2 $4$2 $5";
      var result = sourcestring.replace(re, replacementpattern);
      alert("result = " + result);
    </script>


    $sourcestring after replacement:
    L1 Authors name is ="Replimoi"
    L2 Replimoi wrote the book of all books
    L3 Replimoi painted the childrens illustration chart
    L4 Replimoi coded the biomechanical ultimate progy
    L5 run out of books

     


    Michael

    "In theory, theory and practice are the same. In practice, they are not."
    Albert Einstein
  •  02-05-2010, 10:40 PM 59331 in reply to 59325

    Re: Search and replicate strings

    Thank you Sergei and Michael, you're the best,
    the patterns works like a charm, it solved my problem on targeted lines.

    But I missed to mention that my real problem is global multi-line of electronic card catalog (which in thousands of lines). Same senario, I have to copy a string from the heading (L1) and insert the same string at the beginning of each multi-lines, Say from line (L2) to end of script (it could be line L100000).

    Source String Input:
    L1  Authors name is ="Replimoi"
    L2  wrote the book of all books
    L3  painted the childrens illustration chart
    L4  coded the biomechanical ultimate progy
    ..
    ..
    L100000 last book found


    $sourcestring after replacement:
    Authors name is ="Replimoi"
    Replimoi wrote the book of all books
    ..
    Replimoi last book found



    I apologize for doing a follow up question. But, you guys are my last hope. Thank you again in advance.
    Liz
  •  02-07-2010, 6:33 PM 59380 in reply to 59331

    Re: Search and replicate strings

    Follow-up plead for help, is there any body in their heart of hearts can find kindness and help a poor newbie like me to solve this simple miserable multi-line problem.

     

    Michael’s pattern works great on targeted lines, and I’m totally grateful

     

    Raw Match Pattern:

    (.*?\x22([^\x22]+)\x22.*\s^)(.*\s^)(.*\s^)(.*?\s)

     

    Raw Replace Pattern:

    $1$2 $3$2 $4$2 $5

     

     

    I’m sure this problem is a piece of cake for Regex Masters, that is why I’m begging for any bodies help.

    Any help is welcome, thanks
    Liz
  •  02-07-2010, 6:58 PM 59382 in reply to 59380

    Re: Search and replicate strings

    Liz,

    if you want to do it for thousands and millions of lines, then regex alone is no help , it has to be a relatively simple parsing/text-processing script written in a generic language. The program would actually use a regex to capture the author's name (or whatever string) in run-time and then inserting it into each (?) line. Do you have an exposure to some generic language, like java or c? The algorithm would be simple:

    1. capture the authors' name in a var

    2. open a source file, read it line by line

    3. update a line in a loop by inserting the author name into it

    4. write the line ot a target [modified text] faile

    <end> 

     

  •  02-08-2010, 11:37 AM 59409 in reply to 59382

    Re: Search and replicate strings

    Liz,

    Sergei is right.  What you want to do is not a task for a single regex. This why in the posting guidelines we ask you to explain your ultimate goal. You'll have to write code to accomplish this task.  You could use two regexes to achieve this. Regex #1 find the author name (and replace L1), Regex #2 replace all other L## with the author name.

        <script type="text/javascript">
            function testReplace(){
                var author,
                inputValue = 'L1  Authors name is ="Replimoi"\n\r' +
                'L2  wrote the book of all books\n\r' +
                'L3  painted the childrens illustration chart\n\r' +
                'L4  coded the biomechanical ultimate progy',
                output;
                inputValue = inputValue.replace(/^(?:L1\x20*(.*?\x22([^\x22]+)\x22.*\s*))/, "$1");
                author = RegExp.$2;
                output = inputValue.replace(/^(?:L\d+)/mg, author);
                alert(output);
            }
            
            testReplace();
            </script>


    Michael

    "In theory, theory and practice are the same. In practice, they are not."
    Albert Einstein
View as RSS news feed in XML