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

replace text multi line text

Last post 01-29-2010, 11:44 AM by nirvana74v. 6 replies.
Sort Posts: Previous Next
  •  01-20-2010, 4:36 AM 58630

    replace text multi line text

    Hi

    I am trying to replace text of a string with empty string.

    Travel - Travel - Information
    Animal - Cheetah/Tiger
    Getaway - Getaway - Camping

    This is my regex (\w+\s-\s\w+)$ which works fine for a single line, but for multi line this doesn't seem to be working fine

    What regex should do is replace the text in this manner

     Travel - Travel - Information changes to Travel - Information

    Animal - Cheetah/Tiger remains as it is

    Getaway - Getaway - Camping changes to  Getaway - Camping

     

    Here I have tried traversing text backwards, how is it possible to do the same thing from the beginning of the string?

     

    Thanks in advance,

     

    Ved

     

     

     

  •  01-20-2010, 11:53 AM 58644 in reply to 58630

    Re: replace text multi line text

    You didn't say what programming language or tool you were using so I can't help you with the implementation.

    The following works with the sample you provided.

    Raw Match Pattern:
    ^(\w+\x20-)\x20\1

    Raw Replace Pattern:
    $1

    $sourcestring after replacement:
    Travel - Information
    Animal - Cheetah/Tiger
    Getaway - Camping

     


    Michael

    "In theory, theory and practice are the same. In practice, they are not."
    Albert Einstein
  •  01-20-2010, 11:56 AM 58645 in reply to 58644

    Re: replace text multi line text

    Thanks mash, sorry didn't mention that earlier. I'm using PHP
  •  01-20-2010, 12:37 PM 58652 in reply to 58645

    Re: replace text multi line text

    PHP Code Example: 
    <?php
    $sourcestring="your source string";
    echo preg_replace('/^(\w+\x20-)\x20\1/m','$1',$sourcestring);
    ?>
     

    Michael

    "In theory, theory and practice are the same. In practice, they are not."
    Albert Einstein
  •  01-21-2010, 12:28 AM 58665 in reply to 58652

    Re: replace text multi line text

    The pattern that you wrote doesn't show the results as desired, please refer back to my query again.

    Here is the result of the pattern that you wrote http://www.myregextester.com/?r=db2ca849

    Thanks.

  •  01-21-2010, 11:08 AM 58691 in reply to 58665

    Re: replace text multi line text

    nirvana74v:

    The pattern that you wrote doesn't show the results as desired, please refer back to my query again.

    Here is the result of the pattern that you wrote http://www.myregextester.com/?r=db2ca849

    Thanks.

    No, it works fine. You failed to implement it correctly. If you refer back to my original response you'll not that you failed to use the replace pattern.


    Michael

    "In theory, theory and practice are the same. In practice, they are not."
    Albert Einstein
  •  01-29-2010, 11:44 AM 59085 in reply to 58691

    Re: replace text multi line text

    Thanks Mash for pointing that out, indeed I missed that replacement value.
View as RSS news feed in XML