Spykar
Spykar

Reputation: 39

Need to use Notepad++ RegExp to find and remove characters between curly brackets

Looking to remove the "#" character between {} in the sample text below

Sample Text:

\(C##nH##{##2##n##+##1}O\)

Expected output:

\(C##nH##{2n+1}O\)

What have I tried?

\{[#].*\}
^.+?(?=##\w##)##\K|##(?!$)

Would greatly appreciate your help!

Upvotes: 4

Views: 755

Answers (3)

JvdV
JvdV

Reputation: 75860

I think a possibility would be:

(?:\{|\G(?!^))[^#}]*\K#

And replace with nothing.

See the online demo


  • (?: - Open non-capture group.
    • \{ - Match a literal curly bracket.
    • | - Alternation/OR.
    • \G(?!^) - Assert position at the end of the previous match but not at start of the line.
    • ) - Close non-capturing group.
  • [^#}]* - Match anything but hashtag and closing curly brackets zero or more times.
  • \K- Resets the starting point of the match.
  • # - An hashtag.

Upvotes: 0

41686d6564
41686d6564

Reputation: 19641

You may use the following pattern:

(?:\{|(?<!^)\G)[^#\r\n]*\K#+(?=.*\})

..and replace with an empty string.

Demo.

Breakdown:

(?:             # Beginning of a non-capturing group.
    \{          # Matches the character '{' literally.
    |           # Alternation (OR).
    (?<!^)\G    # Asserts position at the end of the previous match.
)               # End of the non-capturing group.
[^#\r\n]*       # Matches zero or more characters other than '#' or line breaks.
\K              # Resets the starting point of the match (only include what comes next).
#+              # Matches one or more '#' characters.
(?=.*\})        # A positive Lookahead to make sure the `}` character exists.

Upvotes: 2

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626903

You may use

Find What: (\G(?!^)(?=[^}]*})|\{)([^#{}]*)#+
Replace With: $1$2

Details:

  • (\G(?!^)(?=[^}]*})|\{) - Group 1 (referred to with $1 in the replacement pattern): end of the previous match (\G(?!^)) that is followed with any 0+ chars other than } and then followed with } or (|) { char
  • ([^#{}]*) - Group 2 ($2): any 0 or more chars other than #, { and }
  • #+ - one or more # chars

Notepad++ demo & settings:

enter image description here

Upvotes: 1

Related Questions