Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex-based matching and sustitution with nano?

I am aware of nano's search and replace functionality, but is it capable of using regular expressions for matching and substitution (particularly substitutions that use a part of the match)? If so, can you provide some examples of the syntax used (both for matching and replacing)?

I cut my teeth on Perl-style regular expressions, but I've found that text editors will sometimes come up with their own syntax.

like image 593
Daniel Standage Avatar asked Oct 12 '11 19:10

Daniel Standage


People also ask

How does regex substitution work?

Substitutions are language elements that are recognized only within replacement patterns. They use a regular expression pattern to define all or part of the text that is to replace matched text in the input string. The replacement pattern can consist of one or more substitutions along with literal characters.

How do you match expressions in regex?

To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash ( \ ). E.g., \. matches "." ; regex \+ matches "+" ; and regex \( matches "(" . You also need to use regex \\ to match "\" (back-slash).

Is regex matching fast?

Regular expression matching can be simple and fast, using finite automata-based techniques that have been known for decades. In contrast, Perl, PCRE, Python, Ruby, Java, and many other languages have regular expression implementations based on recursive backtracking that are simple but can be excruciatingly slow.

Does regex affect performance?

Being more specific with your regular expressions, even if they become much longer, can make a world of difference in performance. The fewer characters you scan to determine the match, the faster your regexes will be.


3 Answers

My version of nano has an option to swtich to regex search with the meta character + R. In cygwin on Windows, the meta-key is alt, so I hit ctrl+\ to get into search-and-replace mode, and then alt+r to swtich to regex search.

like image 126
user151841 Avatar answered Oct 18 '22 04:10

user151841


You need to add, or un-comment, the following entry in your global nanorc file (on my machine, it was /etc/nanorc):

set regexp

Then fire up a new terminal and press CTRL + / and do your replacements which should now be regex-aware.

EDIT


Search for conf->(\S+):

enter image description here


Replace with \1_conf

enter image description here


Press a to replace all occurrences:

enter image description here


End result:

enter image description here

like image 25
Bart Kiers Avatar answered Oct 18 '22 03:10

Bart Kiers


The regular expression format / notation for nano use "Extended Regular Expression", i.e. POSIX Extended Regular Expression, which is used by egrep and sed -r, this include metacharacters ., [ and ], ^, $, (, ), \1 to \9, *, { and }, ?, +, |, and character classes like [:alnum:], [:alpha:], [:cntrl:], [:digit:], [:graph:], [:lower:], [:print:], [:punct:], [:space:], [:upper:], and [:xdigit:].

For more complete documentation you can see manual page, man 7 regex in Linux or man 7 re_format in OS X. This page may give you same information as well: https://en.wikipedia.org/wiki/Regular_expression#POSIX_basic_and_extended

Unfortunately in nano there seems to be no way to match anything that span across multiple lines.

like image 14
S P Arif Sahari Wibowo Avatar answered Oct 18 '22 02:10

S P Arif Sahari Wibowo