that_javascript_girl
that_javascript_girl

Reputation: 23

Regex negate character without matching it

Here's the data I need to fix:

{skin}\nstarts: head;\ndown to: trunk, extremities\n\nspares: palms, soles

I would like to put a ; in front of the \n to turn it into the following:

{skin};\nstarts: head;\ndown to: trunk, extremities;\nspares: palms, soles

I came up with the following regex expression: /[^;](\\n)+/, but unfortunately it also matches the preceeding character (which is NOT what I want).

Any thoughts on how to proceed??

Upvotes: 0

Views: 71

Answers (3)

Ryszard Czech
Ryszard Czech

Reputation: 18641

Use

;*(\\n)+

Replace with: ;$1.

See regex proof.

EXPLANATION

--------------------------------------------------------------------------------
  ;*                       ';' (0 or more times (matching the most
                           amount possible))
--------------------------------------------------------------------------------
  (                        group and capture to \1 (1 or more times
                           (matching the most amount possible)):
--------------------------------------------------------------------------------
    \\                       '\'
--------------------------------------------------------------------------------
    n                        'n'
--------------------------------------------------------------------------------
  )+                       end of \1 (NOTE: because you are using a
                           quantifier on this capture, only the LAST
                           repetition of the captured pattern will be
                           stored in \1)

Upvotes: 1

Bohemian
Bohemian

Reputation: 425428

You want to replace any number of \n that aren't proceeded by a ; or another \n with ';\n`:

Search: (?<!;)(?<!\\n)\\n+
Replace: ;\\n

See live demo.

Upvotes: 0

rudolfovic
rudolfovic

Reputation: 3286

I almost agree with @anubhava: (?<!;)(\\n)+ I believe the OP wanted to exclude the ; character from the match but not the newlines.

Upvotes: 2

Related Questions