0

I'm just searching in emacs some strings with following pattern:

^*DEFUN*event*$

So i've used grep:

grep -nR "^*DEFUN*event*$" *

but got no match, instead there's a lot of them for example:

DEFUN ("internal-event-symbol-parse-modifiers", Fevent_symbol_parse_modifiers,

What's wrong?

AdminBee
  • 21,637
  • 21
  • 47
  • 71
  • 4
    Does this answer your question? [How do regular expressions differ from wildcards used to filter files](https://unix.stackexchange.com/questions/57957/how-do-regular-expressions-differ-from-wildcards-used-to-filter-files). Your use of `*` is incorrect here, as it doesn't mean "any sequence of characters" but "zero or more of the preceding character". So, you are looking for lines containing `DEFUNevent` or `DEFUNNevent` and the like. – AdminBee Apr 05 '22 at 15:17
  • In short. It should have been `grep -nr 'DEFUN.*event' .` here. – Stéphane Chazelas Apr 05 '22 at 15:21
  • 2
    Given [your question yesterday](https://unix.stackexchange.com/q/697981/133219) and this one today you clearly need to find a tutorial on regular expressions. You could start off with https://www.regular-expressions.info/ but treat it with a pinch of salt as it uses a lot of non-POSIX (i.e. confusing and muddied, especially around "character classes"!) terminology but at least the concepts are there. – Ed Morton Apr 06 '22 at 19:09
  • Actually i'm reading a book, but it's no quite clear. – Quasar999 Apr 07 '22 at 17:33

1 Answers1

2

You are using globbing characters (aka "wildcards") where grep is expecting a regular expression. However, in regular expressions, the * does not mean "any sequence of characters", but "zero or more of the previous character". When it is used as the first character of an expression (or immediately after the "start-of-line" symbol ^), it stands for a literal asterisk.

So, your expression ^*DEFUN*event*$ searches for lines that

  • start with an *
  • immediately continue with DEFU
  • followed by zero or more N
  • followed by the word even
  • and continuing with zero or more t up to the end of the line.

The regular expression you are looking for would be

^.*DEFUN.*event.*$

or in short (as also mentioned by Stéphane Chazelas in a comment)

DEFUN.*event
AdminBee
  • 21,637
  • 21
  • 47
  • 71