1

I would like to replace every _ with a - on lines starting with #| label: using PCRE2 regex within my text editor.

Example:

#| label: my_chunk_label
my_function_name <- function(x)

Should become:

#| label: my-chunk-label
my_function_name <- function(x)

In contrast to .NET regex, where one could substitute (?<=^#\| label: .+)_ with - (regex101 example), PCRE2 does not support infinite lookbehind so the regex is invalid. So far, the only way I found was to repeatedly substitute ^#[^_]+\K_ with - (regex101 example) but I was curious if there is a single-pass solution.

jkd
  • 1,327
  • 14
  • 29

2 Answers2

3

If you are using pcre, you could make use of \G and \K

Then in the replacement use -

(?:^#\|\h+label:\h+|\G(?!^))[^\r\n_]*\K_

The pattern matches:

  • (?: Non capture group for the alternatives
    • ^#\|\h+label:\h+ Match the pattern that should be at the start of the string, where \h matches a horizontal whitespace character
    • | Or
    • \G(?!^) Assert the current position at the end of the previous match, not at the start
  • ) Close the non capture group
  • [^\r\n_]* Match optional characters except for newlines or _
  • \K Forget what is matched so far
  • _ Match the underscore

Regex demo

The fourth bird
  • 154,723
  • 16
  • 55
  • 70
2

An alternative idea is to use PCRE verbs (*SKIP)(*F) to skip the lines that you don't want.

^(?!#\| label:).+(*SKIP)(*F)|_

It's also related to The Trick. On the left side of the alternation lines that don't start with #| label get identified by use of a negative lookahead, matched by .+ and skipped - no replacment wanted in these lines. On the right side matching is done in the remaining lines.

See this demo at regex101 (replace with whatever you like - add variable space if needed)

bobble bubble
  • 16,888
  • 3
  • 27
  • 46