![]() ![]() Grep call unix grep command and put result in a buffer Keep-lines delete all lines except those containing matchesįlush-lines delete lines containing matches How-many count the number of strings matching regexp Multi-occur show lines in all buffers containing a match Highlight-regexp highlight strings matching regexp Query-replace-regexp same, but query before each replacementĪlign-regexp align, using strings matching regexp as delimiters Replace-regexp replace string matching regexp Some Emacs Commands that Use Regular Expressions C-M-s incremental forward search matching regexpĬ-M-r incremental backward search matching regexp ![]() \< \> start/end of word (faulty rendering: backslash + less-than and backslash + greater-than) \_ start/end of symbol \ prevents interpretation of following special char ? previous character or group, repeated 0 or 1 time + previous character or group, repeated 1 or more time * previous character or group, repeated 0 or more time Many characters are special when they follow a backslash – see below. Regular Expression Syntax Regexp Syntax BasicsĪny character matches itself, except for the list below. There’s also a package xr (available on GNU ELPA) that does the opposite conversion, from string syntax regexps to something more human-readable. When used in InteractiveLispMode, Rx can also be a handy tool to construct regexps. To produce this regexp in string format (which matches C-style multiline and single line comments): \\* \\(?. ![]() For example, you can use ‘rx’ like this: (rx (or (and " \*" (*? anything) "*/") (and "//" (*? anything) eol))) However, when writing Lisp code, one can now use the easier to understand Rx Notation, which structures a regular expression as Lisp S-expressions. We use regexps all the time, without knowing it, when we use Emacs.Ĭonventionally, Emacs allows only regular expressions formatted as strings and that is still the case for interactive use, such as ‘M-x’ (command query-replace-regexp). Try ‘C-M-s’ (command isearch-forward-regexp). Regexps are important to Emacs users in many ways, including these: The above regular expression matches “alex” or “alexa”. The question mark, for example, says that the preceding expression (the character “a” in this case) may or may not be present. Some characters have special meanings in a regular expression. The above regular expression matches “alex”. Examples: "alex"Ī plain string is a regular expression that matches the string exactly. It is a pattern that is matched against the text to be searched. Of course, there are better ways to implement regex matching that don’t have horrible run times on craftily-constructed regexes like a.*a.*a.*a.a, but you’ll have to read Russ Cox’s article “Regular Expression Matching Can Be Simple And Fast” for more on that.A regular expression (abbreviated “ regexp” or sometimes just “ re”) is a search-string with wildcards – and more. The simplest way to fix this in the Go version would be to convert the regexp and text strings to slices of runes ( rune) before beginning, and then use the same algorithm from there. and c* won’t match multi-byte characters correctly (though in many cases that won’t matter). Note that neither the C nor the Go version handles Unicode properly. I certainly had fun reading Kernighan’s article, porting the code, and writing this up, so I hope you enjoy it too. I think Pike’s code is useful, instructive, and beautiful. * match: search for regexp anywhere in text */ int match ( char * regexp, char * text ) Conclusion bash_history for grep usage (how meta!) and my percentage is similar, though I also use escaped metacharacters (usually \.) in about 10% of uses. , *, ^, and $, but it’s a well-chosen subset that Kernighan says “easily accounts for 95 percent of all instances” of his day-to-day usage. It handles only a small number of regex metacharacters, namely. Original C versionįirst let’s look at Pike’s original matching code. With Go’s C heritage (and Pike’s influence on the Go language), I thought I’d see how well the C code would translate to Go, and whether it was still elegant. If you haven’t read Kernighan’s “exegesis” of this code, it’s definitely worth the 30-minute time investment it takes to go through that slowly. Back in 1998, Rob Pike – of Go and Plan 9 fame – wrote a simple regular expression matcher in C for The Practice of Programming, a book he wrote with fellow Unix hacker Brian Kernighan. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |