Insert/replace text after a known paragrapher.

32 views
Skip to first unread message

Adriano Borgobello

unread,
May 21, 2020, 8:27:15 AM5/21/20
to BBEdit Talk
In a plain text file, how can I replace or insert a string at the starting of a paragraph, knowing a substring in the previous paragraph?!
I try to explain with an example.
I have hundreds of paragraphs that start with the substring "¶Art. " and continues with different text. I need to insert a new substring "¶_A" at the head of all paragraphs that follows the above, but only at the first (there are undefined number of paragraphs after each "¶Art. " starting paragraph.
I say insert or replace, because I guess I can replace newline char with newline + "¶_A".

Fletcher Sandbeck

unread,
May 21, 2020, 10:18:44 AM5/21/20
to bbe...@googlegroups.com
Something like this should do the trick. The expression in the first set parentheses finds the paragraphs you are targeting. The start of a line ^ followed by your target text and everything up to the next new-line \n. The expression in the second set of parentheses finds the following paragraph. The replacement pattern inserts the new text you need between them so it prefixes the second paragraph.

Search: (^Art\..*?\n+)(.*?$)
Replace: \1_A\2

Art. One
First paragraph
Second paragraph
Art. Two
First paragraph
Second paragraph

Becomes

Art. One
_AFirst paragraph
Second paragraph
Art. Two
_AFirst paragraph
Second paragraph

The Pattern Playground on the Search menu is pretty useful for playing around with these expressions.

Hope this helps,

[fletcher]

--
This is the BBEdit Talk public discussion group. If you have a feature request or need technical support, please email "sup...@barebones.com" rather than posting here. Follow @bbedit on Twitter: <https://twitter.com/bbedit>
---
You received this message because you are subscribed to the Google Groups "BBEdit Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bbedit+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bbedit/522a82e0-4224-415f-803d-dd9862d7d0f0%40googlegroups.com.

Adriano Borgobello

unread,
May 21, 2020, 4:00:54 PM5/21/20
to BBEdit Talk


Il giorno giovedì 21 maggio 2020 16:18:44 UTC+2, flet...@cumuli.com ha scritto:
Something like this should do the trick. The expression in the first set parentheses finds the paragraphs you are targeting. The start of a line ^ followed by your target text and everything up to the next new-line \n. The expression in the second set of parentheses finds the following paragraph. The replacement pattern inserts the new text you need between them so it prefixes the second paragraph.

Search: (^Art\..*?\n+)(.*?$)
Replace: \1_A\2

Thanks, but won't work be me. Instead this does the job and something more (it's not from my skill, but I'm trying to understand it):

Search: ¶([ASCT])([^ ]+ [0-9IVXLC]+)( |\n)
Replace: ¶\1\2\n¶\1_
Reply all
Reply to author
Forward
0 new messages