Showing posts with label regex. Show all posts
Showing posts with label regex. Show all posts

Tuesday, October 25, 2016

Using extended regular expressions in sed

I've never quite figured out Posix regular expressions, and was trying to replace the first n characters for every line in a file the other day using sed. This is easy if all the lines have at least n characters in them.
sed s/^....//

But sed doesn't suppor the "optional" question mark modifier so lines with less than n characters would not be matched.

It turns out you can turn on extended regex support with "-r" or "--regexp-extended" allowing the following.
sed s/^.?.?.?.?//

Wednesday, January 9, 2013

Regex search/replace to insert newlines

I use Kile and Mendeley. Mendeley generates bibtex references, but with no carriage returns, and the carriage returns are apparenly required.

Example:

@article{Baset_2012, place={New York, New York, USA}, ..., pages={1–2}}

I need each of the x={...}, sections to be on a separate line, and it takes a while to do it by hand.

To search/replace, I enter this search criteria:

\}, +([a-zA-Z])
And this replace criteria:

\}, \ \1

The red block is actually representing a CTRL+V of a newline character. Just select an entire empty line, copy and past into the expression. It will look like a blank space.