How can I omit matches followed only a by a newline?

12 views
Skip to first unread message

Chris Lott (fncll)

unread,
Jan 19, 2024, 11:59:39 AMJan 19
to TextSoap
I need to modify some markdown text to replace three hyphens with an em-dash EXCEPT when the three hyphens are on a line by themselves.

How can I write this regex? 

Sample text input:

---
blah blah
blah blah
---

then some more---you know stuff---like this.

---and lines like this
---and such


Should result in all the occurrences of all the --- replaced except the first two:

---
blah blah
blah blah
---

then some more—you know stuff—like this.

—and lines like this  
—and such

Thanks in advance!

c


Mark Munz

unread,
Jan 20, 2024, 8:36:34 PMJan 20
to text...@googlegroups.com
You'll want to use a Regex Find and Replace, like this:

image.png

This searches for three dashes and then the next character is not a return (\n). We capture that non-return value
and then just put it back in the replace result ($1).

If you want to go super fancy, you can use the negative look ahead, but you may have to remember what the syntax is doing each time you go back to it:

image.png

Here the (?! ) is used to indicate we are just making sure that what is inside this group is NOT matched when looking ahead. In our case, \n
I always have to look up the exact syntax for look-ahead/behind and negative look-ahead/behind, so I typically use the first one because 99% of the cases, it works, uses basic matching and has no negative performance aspects. But either works.


--
You received this message because you are subscribed to the Google Groups "TextSoap" group.
To unsubscribe from this group and stop receiving emails from it, send an email to textsoap+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/textsoap/1b434055-e09e-4da8-8271-37d2c01267a0n%40googlegroups.com.


--
Mark Munz
unmarked software
https://textsoap.com/

Reply all
Reply to author
Forward
0 new messages