1

I want to use Notepad++ regex to find all strings that do not match a pattern.

Sample Input Text:

{~Newline~}{~Indent,4~}{~Colour,Blue~}To be or not to be,{~Newline~}{~Indent,6~} {~Colour,Green~}that {~StartItalic~}is{~EndItalic~} the question.{~EndDocument~}

The parts between {~ and ~} are markdown codes. Everything else is plaintext. I want to find all strings which do not have the structure of the markdown, and insert the code {~Plain~} in front of them. The result would look like this:

{~Newline~}{~Indent,4~}{~Colour,Blue~}{~Plain~}To be or not to be,{~Newline~}{~Indent,6~}{~Colour,Green~}{~Plain~}that {~StartItalic~}{~Plain~}is{~EndItalic~}{~Plain~} the question.{~EndDocument~}

The markdown syntax is open-ended, so I can't just use a list of possible codes to not process.

I could insert {~Plain~} after every ~}, then delete every {~Plain~} that's followed by {~, but that seems incredibly clunky.

3
  • This looks like some markup language. I've never seen it before, but probably the same remarks as for HTML and XML apply: it's easier to parse those things properly than to modify them with regex. What markup language is this? Why don't you just parse it, and then process on the level of the markup language, instead of fiddling with characters? Commented Feb 6, 2018 at 18:49
  • It's a language I've invented for a specific purpose. Different types of paragraph in source ASCII get marked-up by Notepad++ macros - headings, blockquotes, bullet points, footnotes etc. A parser translates the result into HTML. Commented Feb 6, 2018 at 18:58
  • Bwoah, that's cool! :D Are you a professional playwright or something like that? Do you have plans to build something like xslt or jquery for that? Commented Feb 6, 2018 at 19:04

2 Answers 2

2

I hope this works with the current version of Notepad++ (don't have it right now).

Matching with:

~}((?:[^{]|(?:{[^~]))+){~

and then replacing by

~}{~Plain~}$1{~

might work. The first group should capture everything between closing ~} and the next {~. It will also match { and } in the text, as long as they are not part of an opening tag {~.

EDIT Additional explanation, so you can modify it better:

~}             end of previous tag
(              start of the "interesting" group that contains text
  (?:          non-capturing group for +
    [^{]       everything except opening braces
    |          OR
    (?:  
      {        opening brace followed by ...
      [^~]       ... some character which is not `~`
    )
  )+           end of non-capturing group for +, repeated 1 or more times
)              end of the "interesting" group
{~             start of the next tag

Here is an interactive example: regex101 example

Sign up to request clarification or add additional context in comments.

5 Comments

The problem here is that the text does contain {, } and ~. But will never contain {~ or ~}. I'd need to search for the specific string ({~), not the ranges [\}] or [\}~]
@Kapitano updated the regex. Now it also accepts { and }, but not {~. Added interactive example
If you do this, you probably want the second group in the replacement string. Does Notepad++ require \} to be escaped? The above worked for pcre flavour.
Yes, closing braces are escaped in Npp. Interesting way to implement AND in regex using OR instead of multiple lookaheads. One small irony - if we assume any tag followed by anything that isn't another tag is followed by plaintext, we can use a simple negative lookahead. Replace (~\})(?!{~) with $0{~Plain~}
Yes, if you add that assumption, you can do that. However, I preferred to actually match the text and capture it in some group, because it gives the possibility to refine what kind of text is to be captured. Is your question now answered?
1

You need to use Negative Lookahead. This regex will match all ~} occurrences, so you can just replace them with ~}{~Plain~}:

~}(?!{~|$)

If you don't want to match the space in {~Indent,6~} {~Colour,Green~}, just use this:

~}(?!{~|$| )

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.