Skip to main content

Posts

Showing posts with the label Regex

Emacs Regular Expressions

I do use GNU Emacs for almost every text file related thing. Functions like search, replace, cut, paste, etc are all quickly mastered. But, then I've faced a different situation. I had to replace a string, repeat some numbers and put another string. The answer: regular expressions. Emacs has a regular expression replace function. To access it, just type: "Ctrl + Alt + Shift + 5" (too much keys huh... ?) And it asks what should be searched. In my case, in which I wanted to capture the regex match, I did type \([0-9][0-9][0-9]\) to locate a 3 digit numbers and capture them. Then I typed enter and did tell Emacs what it should put in those numbers place: ID=\1; mv \1 ./safebox/\1 And enter again and ! to replace all. That is it.

Regex within PostgreSQL

Regex (Regular expressions) are very useful. I had to make a SQL query to return only the firstname from a table where the field provides the full name. ideia_onde=# SELECT name FROM users; name -------------------------------- Linus Torvalds Randal Schwartz Steve Wozniak (3 rows) ideia_onde=# Now, with a simple substring using a regular expression: SELECT SUBSTRING(name, '(.*?(\\s))') FROM users; substring ----------- Linus Randal Steve (3 rows) ideia_onde=# What do I did? The substring function extracts from the name field everything (.*) until (?) a space character (\\s).