Page 1 of 1

Find and replace lines prefixed with a specific character

Posted: Thu Jan 02, 2014 6:08 pm
by Shades
Hi,

I'm new at regular expressions and need a little help to get started. I have several plain text files that contain multiple lines. Some of those lines start and end with a specific character. This could be an asterisk or a letter.

Here's an example:

Polly wants a cracker
Oswald has a cookie but does not share with others
b Carmen goes to school with a lunchbox full of treats b
Michelle doesn't eat potato chips

I would like to find the sentences that have leading and ending characters and remove those. So in the example I'd like to find the sentence b Carmen goes to school with a lunchbox full of treats b and remove the b in the beginning and end of the line.

Can anyone help me with the regex for this?

Re: Find and replace lines prefixed with a specific characte

Posted: Fri Jan 03, 2014 12:39 am
by Shades
Ok, I've found an expression that seems to find the correct line, but I still haven't figured out what to replace it with.

Find: \r\n(b\s)|(\sb)\r\n

Replace: ???

Any help would be greatly appreciated.

Re: Find and replace lines prefixed with a specific characte

Posted: Fri Jan 10, 2014 12:12 pm
by DigitalVolcano
Try this: (with multi-line anchors turned on)

Code: Select all

Regex:
\r\nb(.*)b\r\n

Replace:
\r\n$1\r\n

Re: Find and replace lines prefixed with a specific characte

Posted: Fri Jan 17, 2014 10:04 pm
by Shades
Works like a charm, except for the longer sentences that have a line break in them. But I can catch those by splitting it into 2 separate commands.
Thanks for your help!