Find and replace lines prefixed with a specific character

RegEx problems forum (archived)
Shades
Posts: 4
Joined: Thu Jan 02, 2014 3:20 pm

Find and replace lines prefixed with a specific character

Post 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?
Shades
Posts: 4
Joined: Thu Jan 02, 2014 3:20 pm

Re: Find and replace lines prefixed with a specific characte

Post 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.
User avatar
DigitalVolcano
Site Admin
Posts: 1804
Joined: Thu Jun 09, 2011 10:04 am

Re: Find and replace lines prefixed with a specific characte

Post 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
Shades
Posts: 4
Joined: Thu Jan 02, 2014 3:20 pm

Re: Find and replace lines prefixed with a specific characte

Post 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!
Locked