3
function get_last_word($sentance){
    $wordArr = explode(' ', $sentance);
    $last_word = trim($wordArr[count($wordArr) - 1]);
    runDebug( __FILE__, __FUNCTION__, __LINE__, "Sentance: $sentance. Last word:$last_word",4);
    return $last_word;
}

i want to remove all text between {} result should be:

function get_last_word($sentance){}

i have tried

{+.*}

and its working only when curly brackets are on same line

0

3 Answers 3

12

Newer version of Notepad++ supports multi-line matching (I am now using 6.1.3)

In the Find/Replace dialog, next to the "Regular Expression" radio button, there is a checkbox called ". matches newline" which means multi-line matching.

Then, use \{.*?\} (which is a ungreedy match) to achieve what you want.

Beware that it does not match braces for you. For example

foo {
  bar {
    blabalbla
  }
  xxx {
    yyy
  }
}

will give you

foo {}
  xxx {
    yyy
  }
}

(I believe there are other questions in SO about brace matching in regex, you may have a look, though I wonder if they will work in notepad++)

1
  • multiline matching was the main issue, upgraded to latest version problem solved thanks :)
    – ehmad11
    Jul 27, 2012 at 23:01
6

You should be fine when you just replace \{[^{}]+\} with {}, repeatedly...

Regular expression visualization

3
  • Thank you! @Ωmega. This expression helped me a lot. Even though I never fully understand the meaning.
    – Bob
    Feb 14, 2014 at 22:24
  • @Bob - For better understanding, I have added visualization to my answer. What it says is: Replace {, followed by one or more of characters others then { or }, followed by }, with {}...
    – Ωmega
    Feb 15, 2014 at 14:53
  • 1
    That makes much more sense to me. I have tried to study a little Regular Expression but never use them in such a way. The explanation is very enlightening. Thanks again.
    – Bob
    Feb 19, 2014 at 14:29
2

Try

(?<=\{)[^}]+(?=\})

this will match anything that falls between { and }

1
  • What version are you using? I'm using np++ 6.1.4, checked it with the exact text from the original post.
    – Chris S.
    Jul 27, 2012 at 16:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.