1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Regex: Match all text in a string besides encapsulated text

Discussion in 'General Programming Chat' started by captchaman, Oct 29, 2011.

  1. captchaman

    captchaman Junior Member

    Joined:
    Sep 16, 2010
    Messages:
    190
    Likes Received:
    842
    Occupation:
    Software Programmer
    Location:
    USA
    I'm shit with regex but what I'm trying to achieve is simple: I want to replace a word if found with another word, unless the text is encapsulated, then ignore it.

    I want to turn this:
    Code:
    This is an example. {Examples are} great.
    into this:
    Code:
    This is an replacement text. {Examples are} great.
    How would I achieve this?

    Thanks,
    CM
     
  2. graceworld

    graceworld Newbie

    Joined:
    Oct 21, 2011
    Messages:
    30
    Likes Received:
    3
    Occupation:
    Programmer
    One thing you can try is to replace everything in { } with Id(or something that you know that will not repeat. Then you replace all the text you want. And then you can put all the text back.

    This is an example. {Examples are} great.

    to

    This is an example. Id_1 great.
    (Note that you will have to store Id_1 map to {Examples are})
    to
    This is an replacement text. Id_1 great.
    to
    This is an replacement text. {Examples are} great.
     
    • Thanks Thanks x 1
  3. captchaman

    captchaman Junior Member

    Joined:
    Sep 16, 2010
    Messages:
    190
    Likes Received:
    842
    Occupation:
    Software Programmer
    Location:
    USA
    I already hacked it together so it works, I just wanted to be able to do it the proper way. Thanks anyway.