It is necessary to create a regular expression that will highlight the words msg or oh (any register) in the string, but only those followed by a pair of curly braces with any content inside {} and after all this stands;

for example, this line:

mSg { sda\r\n sadsa\r\n da\r\n}; //dfgdg oh{fsdf}; jj msg {ddd msg msg { sda\r\n sadsa\r\n da\r\n}; //dfgdfg 

I tried this:

 (msg|oh)(?=(\s*{(\s|.)*}\s*;)) 

comes out the following:

 mSg //Π²Ρ‹Π΄Π΅Π»ΠΈΠ»ΠΎΡΡŒ (ΠΊΠ°ΠΊ ΠΈ Π΄ΠΎΠ»ΠΆΠ½ΠΎ) { sda\r\n sadsa\r\n da\r\n}; //dfgdg oh{fsdf}; jj //Π²Ρ‹Π΄Π΅Π»ΠΈΠ»ΠΎΡΡŒ (ΠΊΠ°ΠΊ ΠΈ Π΄ΠΎΠ»ΠΆΠ½ΠΎ) msg {ddd //Π½ΠΎ Π΅Ρ‰Ρ‘ Π²Ρ‹Π΄Π΅Π»ΠΈΠ»ΠΎΡΡŒ это (Π° Π½Π΅ Π΄ΠΎΠ»ΠΆΠ½ΠΎ, вСдь Π½Π΅Ρ‚ Π·Π°ΠΊΡ€Ρ‹Π²Π°ΡŽΡ‰Π΅ΠΉ скобки) msg //Π½Π΅ Π²Ρ‹Π΄Π΅Π»ΠΈΠ»ΠΎΡΡŒ (ΠΊΠ°ΠΊ ΠΈ Π΄ΠΎΠ»ΠΆΠ½ΠΎ) msg { sda\r\n //Π²Ρ‹Π΄Π΅Π»ΠΈΠ»ΠΎΡΡŒ (ΠΊΠ°ΠΊ ΠΈ Π΄ΠΎΠ»ΠΆΠ½ΠΎ) sadsa\r\n da\r\n}; //dfgdfg 

    1 answer 1

    Your mistake is that the forward condition (? = ...) - you can not do a check for an indefinite length. Ie: + or *

    Something like this:

     "/(oh|msg)\s*(?=\{)(?:\{[^\{]*?\})/gis" 

    Select from your example all msg and oh , followed by " { " and the closing brace is present

    Flags:
    g - global search
    i - case-insensitive
    s - read text in one line

    Living example: link

    • @ololo, I did not read your question at the beginning. Corrected his answer - HA3IK